[1.0/1.1] impersonation issue
Hi,
Code1 ---> c# web application
HTML Code:
class class1
{
Impersonation;
//code here
undo Impersonation;
class 2
{
Impersonation;
//code here
undo Impersonation;
}
}
Code2 ---> c# web application
HTML Code:
class class1
{
Impersonation;
//code here
class 2
{
//code here
}
//undo Impersonation;
}
Code 1 and code 2 working properly. the only issue I found in code 1 is very slow during exection.
My question in code 2 is it ok , not to undo the impersonation? Is there no problem in memory? If I uncomment the undo impersonation in code 2. there is an error occor. It say it cannot complete the operation.
Pls give me an input.
thanks,
Popkie
Re: [1.0/1.1] impersonation issue
Show how you're doing and undoing the Impersonation.
Re: [1.0/1.1] impersonation issue
hi hackmend,
this is the method i used to call inside my class.
impersonate.Logoonimpersonate(); // impersonation method
impersonate.Logoffimpersonate(); // undo impersonation
belows is the impersonation class
Code:
using System;
using System.Runtime.InteropServices;
namespace WebPartControls
{
/// <summary>
/// Summary description for impersonate.
/// </summary>
public class impersonate
{
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_LOGON_BATCH = 4;
const int LOGON32_LOGON_SERVICE = 5;
const int LOGON32_LOGON_UNLOCK = 7;
const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
const int LOGON32_PROVIDER_DEFAULT = 0;
static IntPtr lnToken;
[DllImport("advapi32.dll", SetLastError=true)]
public static extern int LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken
);
[DllImport("advapi32.dll", SetLastError=true)]
public static extern int ImpersonateLoggedOnUser(
IntPtr hToken
);
[DllImport("advapi32.dll", SetLastError=true)]
static extern int RevertToSelf();
[DllImport("kernel32.dll", SetLastError=true)]
static extern int CloseHandle(IntPtr hObject);
public static void Logoonimpersonate()
{
IntPtr lnToken;
int TResult = LogonUser("username","domain","password", LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
out lnToken);
ImpersonateLoggedOnUser(lnToken);
}
public impersonate(string userid,string domain,string password)
{
int TResult = LogonUser(userid,domain,password,
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
out lnToken);
ImpersonateLoggedOnUser(lnToken);
}
public static void Logoffimpersonate()
{
RevertToSelf();
}
}
}