Home All Groups Group Topic Archive Search About
Author
12 May 2005 8:33 AM
Pramod
Hi,


I have the following Impersonation code. This code is in C# and I want
to retrieve the data from the SQL server database using the Specified
windows username and password.


public bool ImpersonateUser(string sUsername, string sDomain, string
sPassword)
{
   bool bImpersonated = false;
   pExistingTokenHandle = new IntPtr(0);
   pExistingTokenHandle = IntPtr.Zero;


   try
   {
     const int LOGON32_PROVIDER_DEFAULT = 0;


     // create token
     //const int LOGON32_LOGON_INTERACTIVE = 2;


     const int LOGON32_LOGON_NETWORK = 3;
     //const int SecurityImpersonation = 2;


     // get handle to token
     bImpersonated = LogonUser(sUsername, sDomain, sPassword,
           LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
           ref pExistingTokenHandle);
   }
   catch (Exception e)
   {
      ATrace.WriteLine(   ATrace.CATEGORY.ERROR,
"SQLQueryBuilder::ImpersonateU­­ser - " + e.Message );
   }
   return bImpersonated;



}


This code does not throw any exception but returns false.

Interesting thing is, I have created a sample application similar to
this in ASP.Net. The same application works but not mine which is ther
in project.


So, is ther any prequisite to run that code ???
Or is ther any project settings ???


If you know, then plz let me know ASAP


Thanks,
Pramod.

Author
12 May 2005 2:54 PM
Joe Kaplan (MVP - ADSI)
I would highly recommend you use the canonical sample code for programmatic
impersonation provided by MS here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemSecurityPrincipalWindowsImpersonationContextClassTopic.asp?frame=true

It shows proper use of Marshal.GetLastWin32Error to find out what really
went wrong.

Also, if you are trying to run this code on Windows 2000 and getting an
error regarding privileges, you should read the documentation here that
explains the problem:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/logonuser.asp?frame=true

Joe K.

Show quoteHide quote
"Pramod" <pram***@cybage.com> wrote in message
news:eRKJFzsVFHA.3488@TK2MSFTNGP10.phx.gbl...
> Hi,
>
>
> I have the following Impersonation code. This code is in C# and I want
> to retrieve the data from the SQL server database using the Specified
> windows username and password.
>
>
> public bool ImpersonateUser(string sUsername, string sDomain, string
> sPassword)
> {
>   bool bImpersonated = false;
>   pExistingTokenHandle = new IntPtr(0);
>   pExistingTokenHandle = IntPtr.Zero;
>
>
>   try
>   {
>     const int LOGON32_PROVIDER_DEFAULT = 0;
>
>
>     // create token
>     //const int LOGON32_LOGON_INTERACTIVE = 2;
>
>
>     const int LOGON32_LOGON_NETWORK = 3;
>     //const int SecurityImpersonation = 2;
>
>
>     // get handle to token
>     bImpersonated = LogonUser(sUsername, sDomain, sPassword,
>           LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
>           ref pExistingTokenHandle);
>   }
>   catch (Exception e)
>   {
>      ATrace.WriteLine(   ATrace.CATEGORY.ERROR,
> "SQLQueryBuilder::ImpersonateU­­ser - " + e.Message );
>   }
>   return bImpersonated;
>
>
>
> }
>
>
> This code does not throw any exception but returns false.
>
> Interesting thing is, I have created a sample application similar to
> this in ASP.Net. The same application works but not mine which is ther
> in project.
>
>
> So, is ther any prequisite to run that code ???
> Or is ther any project settings ???
>
>
> If you know, then plz let me know ASAP
>
>
> Thanks,
> Pramod.
>
>
>

Bookmark and Share