- [DllImport("advapi32.dll", SetLastError=true)]
- public extern static bool LogonUser(String lpszUsername, String
- lpszDomain,
- String lpszPassword, int dwLogonType,
- int dwLogonProvider, ref IntPtr phToken);
-
-
-
- public void Impersonate()
- {
-
- const int LOGON32_LOGON_INTERACTIVE = 2;
- const int LOGON32_LOGON_NETWORK = 3;
-
- const int LOGON32_PROVIDER_DEFAULT = 0;
- const int LOGON32_PROVIDER_WINNT35 = 1;
- const int LOGON32_PROVIDER_WINNT40 = 2;
- const int LOGON32_PROVIDER_WINNT50 = 3;
-
-
-
- IntPtr UserToken = new IntPtr(0);
- bool loggedOn;
-
- try
- {
- //tente de logger l'utilisateur
- loggedOn = LogonUser(
- "LOGIN",
- "DOMAINE",//Dns.GetHostName(),
- MOT DE PASSE",
- LOGON32_LOGON_NETWORK,
- LOGON32_PROVIDER_DEFAULT,
- ref UserToken);
- }
- catch(Exception ex)
- {
- throw ex;
- }
-
- if(loggedOn) //logging ok ?
- {
- //renvoi identité ASP_NET
- WindowsIdentity ident_here1 = WindowsIdentity.GetCurrent();
-
-
- WindowsIdentity SystemMonitorUser = new WindowsIdentity(UserToken);
-
- //Changement d'utilisateur ici
- WindowsImpersonationContext ImpersonatedUser =SystemMonitorUser.Impersonate();
-
-
- //ridentité nouvel User
- WindowsIdentity ident_here2 = WindowsIdentity.GetCurrent();
-
-
- /************************************************
- *
- *
- * EXECUTER LE CODE ICI
- *
- *
- ************************************************/
-
-
- ImpersonatedUser.Undo();
- }
- }
[DllImport("advapi32.dll", SetLastError=true)]
public extern static bool LogonUser(String lpszUsername, String
lpszDomain,
String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
public void Impersonate()
{
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT35 = 1;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT50 = 3;
IntPtr UserToken = new IntPtr(0);
bool loggedOn;
try
{
//tente de logger l'utilisateur
loggedOn = LogonUser(
"LOGIN",
"DOMAINE",//Dns.GetHostName(),
MOT DE PASSE",
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
ref UserToken);
}
catch(Exception ex)
{
throw ex;
}
if(loggedOn) //logging ok ?
{
//renvoi identité ASP_NET
WindowsIdentity ident_here1 = WindowsIdentity.GetCurrent();
WindowsIdentity SystemMonitorUser = new WindowsIdentity(UserToken);
//Changement d'utilisateur ici
WindowsImpersonationContext ImpersonatedUser =SystemMonitorUser.Impersonate();
//ridentité nouvel User
WindowsIdentity ident_here2 = WindowsIdentity.GetCurrent();
/************************************************
*
*
* EXECUTER LE CODE ICI
*
*
************************************************/
ImpersonatedUser.Undo();
}
}