Impersonation code

P

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.
 
T

TE

Hi,

I have not checked your code, but as far as I know the executing user of the
LogonUser API needs to be granted the "Act as part of OS" right. (I think
this is a Windows2000 Issue only).

Regards

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top