WindowsIdentity.RunImpersonated Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Överlagringar
| Name | Description |
|---|---|
| RunImpersonated(SafeAccessTokenHandle, Action) |
Kör den angivna åtgärden som den personifierade Windows identiteten. I stället för att använda ett personifierat metodanrop och köra funktionen i WindowsImpersonationContextkan du använda RunImpersonated(SafeAccessTokenHandle, Action) och ange funktionen direkt som en parameter. |
| RunImpersonated<T>(SafeAccessTokenHandle, Func<T>) |
Kör den angivna funktionen som den personifierade Windows identiteten. I stället för att använda ett personifierat metodanrop och köra funktionen i WindowsImpersonationContextkan du använda RunImpersonated(SafeAccessTokenHandle, Action) och ange funktionen direkt som en parameter. |
RunImpersonated(SafeAccessTokenHandle, Action)
Kör den angivna åtgärden som den personifierade Windows identiteten. I stället för att använda ett personifierat metodanrop och köra funktionen i WindowsImpersonationContextkan du använda RunImpersonated(SafeAccessTokenHandle, Action) och ange funktionen direkt som en parameter.
public:
static void RunImpersonated(Microsoft::Win32::SafeHandles::SafeAccessTokenHandle ^ safeAccessTokenHandle, Action ^ action);
public static void RunImpersonated(Microsoft.Win32.SafeHandles.SafeAccessTokenHandle safeAccessTokenHandle, Action action);
static member RunImpersonated : Microsoft.Win32.SafeHandles.SafeAccessTokenHandle * Action -> unit
Public Shared Sub RunImpersonated (safeAccessTokenHandle As SafeAccessTokenHandle, action As Action)
Parametrar
- safeAccessTokenHandle
- SafeAccessTokenHandle
SafeAccessTokenHandle för den personifierade Windows identiteten.
- action
- Action
System.Action som ska köras.
Exempel
I följande exempel visas hur klassen används WindowsIdentity för att personifiera en användare.
Varning
I det här exemplet uppmanas användaren att ange ett lösenord på konsolskärmen. Lösenordet visas på skärmen eftersom konsolfönstret inte stöder maskerade indata internt.
// The following example demonstrates the use of the WindowsIdentity class to impersonate a user.
// IMPORTANT NOTE:
// This sample asks the user to enter a password on the console screen.
// The password will be visible on the screen, because the console window
// does not support masked input natively.
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using Microsoft.Win32.SafeHandles;
public class ImpersonationDemo
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeAccessTokenHandle phToken);
public static void Main()
{
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a user on this machine.
Console.Write("Enter the name of the domain on which to log on: ");
string domainName = Console.ReadLine();
Console.Write("Enter the login of a user on {0} that you wish to impersonate: ", domainName);
string userName = Console.ReadLine();
Console.Write("Enter the password for {0}: ", userName);
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
// Call LogonUser to obtain a handle to an access token.
SafeAccessTokenHandle safeAccessTokenHandle;
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeAccessTokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine("LogonUser failed with error code : {0}", ret);
throw new System.ComponentModel.Win32Exception(ret);
}
Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
// Check the identity.
Console.WriteLine("Before impersonation: " + WindowsIdentity.GetCurrent().Name);
// Note: if you want to run as unimpersonated, pass
// 'SafeAccessTokenHandle.InvalidHandle' instead of variable 'safeAccessTokenHandle'
WindowsIdentity.RunImpersonated(
safeAccessTokenHandle,
// User action
() =>
{
// Check the identity.
Console.WriteLine("During impersonation: " + WindowsIdentity.GetCurrent().Name);
}
);
// Check the identity again.
Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name);
}
}
Kommentarer
Note
Den här metoden kan användas på ett tillförlitligt sätt med mönstret async/await, till skillnad från Impersonate. I en asynkron metod kan den allmänna överbelastningen av den här metoden användas med ett asynkront ombudsargument så att den resulterande aktiviteten kan vänta.
Gäller för
RunImpersonated<T>(SafeAccessTokenHandle, Func<T>)
Kör den angivna funktionen som den personifierade Windows identiteten. I stället för att använda ett personifierat metodanrop och köra funktionen i WindowsImpersonationContextkan du använda RunImpersonated(SafeAccessTokenHandle, Action) och ange funktionen direkt som en parameter.
public:
generic <typename T>
static T RunImpersonated(Microsoft::Win32::SafeHandles::SafeAccessTokenHandle ^ safeAccessTokenHandle, Func<T> ^ func);
public static T RunImpersonated<T>(Microsoft.Win32.SafeHandles.SafeAccessTokenHandle safeAccessTokenHandle, Func<T> func);
static member RunImpersonated : Microsoft.Win32.SafeHandles.SafeAccessTokenHandle * Func<'T> -> 'T
Public Shared Function RunImpersonated(Of T) (safeAccessTokenHandle As SafeAccessTokenHandle, func As Func(Of T)) As T
Typparametrar
- T
Den typ av objekt som används av och returneras av funktionen.
Parametrar
- safeAccessTokenHandle
- SafeAccessTokenHandle
SafeAccessTokenHandle för den personifierade Windows identiteten.
- func
- Func<T>
System.Func att köra.
Returer
Resultatet av funktionen.
Exempel
I följande exempel visas hur klassen används WindowsIdentity för att personifiera en användare.
Varning
I det här exemplet uppmanas användaren att ange ett lösenord på konsolskärmen. Lösenordet visas på skärmen eftersom konsolfönstret inte stöder maskerade indata internt.
// The following example demonstrates the use of the WindowsIdentity class to impersonate a user.
// IMPORTANT NOTE:
// This sample asks the user to enter a password on the console screen.
// The password will be visible on the screen, because the console window
// does not support masked input natively.
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using Microsoft.Win32.SafeHandles;
public class ImpersonationDemo
{
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out SafeAccessTokenHandle phToken);
public static void Main()
{
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a user on this machine.
Console.Write("Enter the name of the domain on which to log on: ");
string domainName = Console.ReadLine();
Console.Write("Enter the login of a user on {0} that you wish to impersonate: ", domainName);
string userName = Console.ReadLine();
Console.Write("Enter the password for {0}: ", userName);
const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;
// Call LogonUser to obtain a handle to an access token.
SafeAccessTokenHandle safeAccessTokenHandle;
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeAccessTokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine("LogonUser failed with error code : {0}", ret);
throw new System.ComponentModel.Win32Exception(ret);
}
Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
// Check the identity.
Console.WriteLine("Before impersonation: " + WindowsIdentity.GetCurrent().Name);
// Note: if you want to run as unimpersonated, pass
// 'SafeAccessTokenHandle.InvalidHandle' instead of variable 'safeAccessTokenHandle'
WindowsIdentity.RunImpersonated(
safeAccessTokenHandle,
// User action
() =>
{
// Check the identity.
Console.WriteLine("During impersonation: " + WindowsIdentity.GetCurrent().Name);
}
);
// Check the identity again.
Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name);
}
}
Kommentarer
Note
Den här metoden kan användas på ett tillförlitligt sätt med mönstret async/await, till skillnad från Impersonate. I en asynkron metod kan den här metoden användas med ett asynkront ombudsargument så att den resulterande uppgiften kan vänta.