2011-10-10 31 views
9
WindowsIdentity identity = new WindowsIdentity(accessToken); 
WindowsImpersonationContext context = identity.Impersonate(); 

... 
context.Undo(); 

Dove si dichiara un utente Nome utente e Passowrd?Impersonare con nome utente e password?

il access token param non mi troppo aiuta ...

Devo importare DLL per questo?

risposta

22

è necessario per ottenere il token dell'utente. Utilizzare il P/Invoke LogonUser dal advapi32.dll:

[DllImport("advapi32.dll", SetLastError = true)] 
    public static extern bool LogonUser(
      string lpszUsername, 
      string lpszDomain, 
      string lpszPassword, 
      int dwLogonType, 
      int dwLogonProvider, 
      out IntPtr phToken); 

Esempio:

IntPtr userToken = IntPtr.Zero; 

bool success = External.LogonUser(
    "john.doe", 
    "domain.com", 
    "MyPassword", 
    (int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2 
    (int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0 
    out userToken); 

if (!success) 
{ 
    throw new SecurityException("Logon user failed"); 
} 

using (WindowsIdentity.Impersonate(userToken)) 
{ 
    // do the stuff with john.doe's credentials 
} 
+0

C'è un modo per farlo senza la password? Ho accesso ad esso mentre sto creando proprio prima della rappresentazione, ho pensato di chiedere. – Doug

+2

Immagino che si dovrebbe chiamare 'CloseHandle' (come menzionato nella [documentazione per' LogonUser'] (https://msdn.microsoft.com/en-us/library/windows/desktop/aa378184 (v = vs.85) .aspx)) per il 'userToken' dopo il blocco using. O è chiamato in qualche modo da 'WindowsIdentity'? – CodeFox

+0

Salve Se questa è un'applicazione ASP.NET, qual è lo scopo di questo? Devo chiamare questa funzione in ogni pagina? –

2

È necessario P/invocare l'API LogonUser(). Accetta nome utente, dominio e password e restituisce un token.

5

esattamente il token di accesso che è necessario utilizzare. per ottenerlo è necessario chiamare il metodo LogonUser:

oops non ha capito che ho solo il codice VB.net proprio qui. immaginare che in C#;) qui a c#

dichiarazione di metodo esterno:

Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal lpszUsername As [String], _ 
ByVal lpszDomain As [String], ByVal lpszPassword As [String], _ 
ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, _ 
ByRef phToken As IntPtr) As Boolean 

e l'esecuzione:

_Token = New IntPtr(0) 

Const LOGON32_PROVIDER_DEFAULT As Integer = 0 
'This parameter causes LogonUser to create a primary token. 
Const LOGON32_LOGON_INTERACTIVE As Integer = 2 
Const LOGON32_LOGON_NEWCREDENTIALS As Integer = 9 

_Token = IntPtr.Zero 

' Call LogonUser to obtain a handle to an access token. 
Dim returnValue As Boolean = LogonUser(_User, _Domain, _Password, LOGON32_LOGON_NEWCREDENTIALS, LOGON32_PROVIDER_DEFAULT, _Token) 

If False = returnValue Then 
    Dim ret As Integer = Marshal.GetLastWin32Error() 
    Console.WriteLine("LogonUser failed with error code : {0}", ret) 
    Throw New System.ComponentModel.Win32Exception(ret) 
End If 

_Identity = New WindowsIdentity(_Token) 
_Context = _Identity.Impersonate() 
Problemi correlati