 |
|
 |
How can i call LogonUser with a username "something" and without any password?
|
|
|
|
 |
|
 |
I have this code:
public class Credenciales : IDisposable
{
[DllImport("advapi32.dll", SetLastError=true)]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
[DllImport( "kernel32", SetLastError = true )]
private static extern bool CloseHandle(IntPtr hObject);
private IntPtr userHandle = IntPtr.Zero;
private WindowsImpersonationContext impersonationContext;
public Credenciales( string user, string domain, string password )
{
if ( ! string.IsNullOrEmpty( user ) )
{
bool loggedOn = LogonUser( user, domain, password,
9 ,
3 ,
out userHandle );
if ( !loggedOn )
throw new Win32Exception( Marshal.GetLastWin32Error() );
impersonationContext = WindowsIdentity.Impersonate( userHandle );
}
}
public void Dispose()
{
if ( userHandle != IntPtr.Zero )
CloseHandle( userHandle );
if ( impersonationContext != null )
impersonationContext.Undo();
}
}
And also I have installed BDE Administrator for Paradox Data Bases.
My problem is that when a user without administrator privilegies want to connect to DB, it's not working and I think it is because the user can't load DLLs.
Is any solution for my problem?
These is my other code:
public int RZb()
{
xml = new XML();
try
{
string myConnectionString = "Driver={Microsoft Paradox Driver (*.db )};" + "fil=Paradox 7.x;" + "driverid=538;" + "collatingsequence=ASCII;" + "dbq=" + xml.Recuperar("RelojBBDD") + ";" + "defaultdir=" + xml.Recuperar("RelojBBDD") + ";" + "paradoxnetpath=" + xml.Recuperar("RelojBBDDNetFiles") + ";" + "paradoxnetstyle=4.x;" + "paradoxusername=admin;" + "safetransactions=0;" + "threads=3;" + "uid=admin;" + "usercommitsync=Yes";
OdbcConnection myConnection = new OdbcConnection();
myConnection.ConnectionString = myConnectionString;
myConnection.Open();
OdbcCommand DbCommand = myConnection.CreateCommand();
DbCommand.CommandText = "SELECT codigo, Nombre, DNI FROM Personal WHERE Baja=0 AND DNI LIKE '%" + ((Main)(this.Parent.Parent)).lblDNI.Text.Substring(0,8) + "%';";
OdbcDataReader DbReader = DbCommand.ExecuteReader();
OdbcDataAdapter da = new OdbcDataAdapter(DbCommand);
DataSet dsRetrievedData = new DataSet();
myConnection.Close();
da.Fill(dsRetrievedData);
DataRowCollection dra = dsRetrievedData.Tables["Table"].Rows;
if (dra.Count == 0)
{
myConnectionString = "Driver={Microsoft Paradox Driver (*.db )};" + "fil=Paradox 7.x;" + "driverid=538;" + "collatingsequence=ASCII;" + "dbq=" + xml.Recuperar("RelojUdaltzaingoBBDD") + ";" + "defaultdir=" + xml.Recuperar("RelojUdaltzaingoBBDD") + ";" + "paradoxnetpath=" + xml.Recuperar("RelojUdaltzaingoBBDDNetFiles") + ";" + "paradoxnetstyle=4.x;" + "paradoxusername=admin;" + "safetransactions=0;" + "threads=3;" + "uid=admin;" + "usercommitsync=Yes";
myConnection = new OdbcConnection();
myConnection.ConnectionString = myConnectionString;
myConnection.Open();
DbCommand = myConnection.CreateCommand();
DbCommand.CommandText = "SELECT codigo, Nombre, DNI FROM Personal WHERE Baja=0 AND DNI LIKE '%" + ((Main)(this.Parent.Parent)).lblDNI.Text.Substring(0, 8) + "%';";
DbReader = DbCommand.ExecuteReader();
da = new OdbcDataAdapter(DbCommand);
dsRetrievedData = new DataSet();
myConnection.Close();
da.Fill(dsRetrievedData);
dra = dsRetrievedData.Tables["Table"].Rows;
}
foreach (DataRow dr in dra)
{
RelojZb = int.Parse(dr["codigo"].ToString());
}
return RelojZb;
}
catch (Exception ex)
{
MessageBox.Show("Erroreak egon dira markajeekin edota beste erabiltzaile bat dago koltsulta egiten");
return 0;
}
}
|
|
|
|
 |
|
|
 |
|
 |
Thanks for the reply.
I think the problem is the computer and the accounts, because with an old account it doesn't work but if we copy the privileges of the account to a new account it works fine, so I don't know which is the problem with all of this.
It can be because the accounts are old and when it was created it was done with a winNT version and not with the winXP SP3, or is a problem of cache files in the computer...
In some computers works perfectly, in others only works with some users, normally old accounts. This is a mysterious.
|
|
|
|
 |
|
 |
Hello, din't find any info.
I am planning to use Windows Impersionation in a multi threaded Server Application.
Does anyone know if an impersionation affects other threads, or does it exist only for the Thread where the impersionation have been made ??
|
|
|
|
 |
|
 |
Hi,
thanks for providing this code. it was helpful.
|
|
|
|
 |
|
 |
Hello, I Try this method in windows 7 and not work, somebody have an update for run this one on Windows 7?
|
|
|
|
 |
|
 |
Hi,
I am trying to use this application to impersonate the user across forest. There is no trust between the forests. This code does NOT seem to work in cross forest environment. Is this a known limitation of this app?
Atul Sureka
|
|
|
|
 |
|
 |
I'm facing the same problem as well. Any idea how to resolve cross-domain impersonation?
|
|
|
|
 |
|
|
 |
|
 |
Hi,
Thanks for the demo.
I am trying to create a folder on a remote computer that I know its credentials.
The parent folder is not shared.
So I did what you wrote (with the exception that I used the "SecurityDelegation").
It didn't work.
I don't understand why you do "DuplicateToken" (why can't I use the token that it returned from the LogonUser())?
I also don't understand why when I give the wrong user-name/password I stikk get no error and a valid token.
Please note that when I used anything else than LOGON32_LOGON_NEW_CREDENTIALS I always rectved error 1326.
I wrote it in C++ so what I did is
if (!LogonUser(_T("test"),_T("\\\\SHLOMOARAN"),_T("abc"),LOGON32_LOGON_NEW_CREDENTIALS,LOGON32_PROVIDER_DEFAULT,&hToken))
{
// error
}
if (!DuplicateToken(hToken,SecurityDelegation,&hNewToken))
{
// error
}
ImpersonateLoggedOnUser(hNewToken);
if (!hNewToken)
{
// error
}
if (CreateDirectory(path,NULL))
{
// error
}
|
|
|
|
 |
|
 |
Hi Does this code work with windows 2008 server. I am getting command failed error . Can you please help.
Thanks,
Smitha.
|
|
|
|
 |
|
 |
I have tried the same process both in forms and WPF applications unfortunately am failed to impersonate user in WPF application.
Did i missed anything while impersonating a user in WPF application?
|
|
|
|
 |
|
 |
This was written in 2003 using the 1.0 Framework version. It's no longer necessary to use P/Invoke for impersonation. Take a look at System.Threading.Thread.CurrentPrincipal.
|
|
|
|
 |
|
 |
Well I've done a lot of searching about how to use System.Threading.Thread.CurrentPrincipal for impersonation and still haven't found a single place where the process is described.
Please help, if you have any clues of how to perform impersonation wihout P/Invoke ...
Regards.
|
|
|
|
 |
|
 |
Hi,
It's a nice work. Congratulations!
But,
I would like to run a small windows service application.
It will make some tasks and than the service may run another
process but with other local credentials.
The main service will run under SYSTEM account.
Can you help me?
Thanks!
Willian S. Rodrigues
willian_cpp_br@hotmail.com
|
|
|
|
 |
|
 |
I would like to have this funcionality in an ActiveX running in Windows CE .net
Do you know if this is possible?
Thanks,
Antonio.
Antonio
|
|
|
|
 |
|
 |
The compact framework has no support for the WindowsImpersonationContext class that I see, so the short answer would be no. However, I'm sure there are plenty of ways to achieve this using a P/Invoke approach.
|
|
|
|
 |
|
 |
did you find a way out to run on Windows CE
|
|
|
|
 |
|
 |
Hi,
I've tried to use your code, or at least some of it in my project, and I've encountered the following problem:
The code runs, impersonates finely on local computer, but when I try to impersonate a user on an other computer (e.g. "Comp" , "User", "") I get error code 1326, the domain IS "Comp", username IS "User" and there is no password on it. So I don't know the solution for my problem.
I've tried your sample application, which encountered the same.
Could you provide me some information about this?
OK, I know that if there is no passwd on the share I needn't use this code, but the use of it should not end in the result written above, or is it?
After trying I noticed, that this program impersonates on local computer, because when I've tried to impersonate an admin user on an other computer, which has the same name and passwd as the admin of the local computer, it impersonated on the local computer. Is it possible or I'm missunderstanding something?
thanks in advance: collapo
-- modified at 7:13 Tuesday 21st August, 2007
Thx in advance:
Collapo
|
|
|
|
 |
|
 |
Hi
It is possible. You use mirrored accounts. In this way you can have access to local resources on the OTHER machine. But the question is how to achieve this (ex. access to files on other machines with NTFS rights sets on local account) without creating mirrored accounts on both?
Mirek
|
|
|
|
 |
|
 |
Hi,
i have tried everything and i get the same error.
Tried this :
I'm loged on the domain "AAA" with a user "A" and trying to log on the domain "AAA" as "B". Error 1326
tried this also :
I'm loged on the domain "AAA" with a user "A". I have created a local account "M" and tried log on the domain "localhost" as "localhost\M".
any ideas ?
|
|
|
|
 |
|
 |
Hi,
I'm using Win 2000 Server and the sample application return:
LogonUser() failed with error code: 1314
and then:
DuplicateToken() failed with error code: 6
This error encurred only in a machine, in other machine works fine. I search the code on web and I think that I have to set some permission. But I don't know what.
Please help me, thanks Matteo
|
|
|
|
 |
|
 |
I'm sorry, setting the permission "Act as part of the operating system" works fine. Sorry again
|
|
|
|
 |
|
 |
How do you do that? Can you show me an example.
|
|
|
|
 |