WindowsIdentity.RunImpersonated WindowsIdentity.RunImpersonated WindowsIdentity.RunImpersonated Method

정의

오버로드

RunImpersonated(SafeAccessTokenHandle, Action) RunImpersonated(SafeAccessTokenHandle, Action) RunImpersonated(SafeAccessTokenHandle, Action)

가장된 Windows ID로 지정된 작업을 실행합니다.Runs the specified action as the impersonated Windows identity. 가장된 메서드 호출을 사용하여 WindowsImpersonationContext에서 함수를 실행하지 않고 RunImpersonated(SafeAccessTokenHandle, Action)를 사용하여 매개 변수로 직접 함수를 제공할 수 있습니다.Instead of using an impersonated method call and running your function in WindowsImpersonationContext, you can use RunImpersonated(SafeAccessTokenHandle, Action) and provide your function directly as a parameter.

RunImpersonated<T>(SafeAccessTokenHandle, Func<T>) RunImpersonated<T>(SafeAccessTokenHandle, Func<T>) RunImpersonated<T>(SafeAccessTokenHandle, Func<T>)

가장된 Windows ID로 지정된 함수를 실행합니다.Runs the specified function as the impersonated Windows identity. 가장된 메서드 호출을 사용하여 WindowsImpersonationContext에서 함수를 실행하지 않고 RunImpersonated(SafeAccessTokenHandle, Action)를 사용하여 매개 변수로 직접 함수를 제공할 수 있습니다.Instead of using an impersonated method call and running your function in WindowsImpersonationContext, you can use RunImpersonated(SafeAccessTokenHandle, Action) and provide your function directly as a parameter.

RunImpersonated(SafeAccessTokenHandle, Action) RunImpersonated(SafeAccessTokenHandle, Action) RunImpersonated(SafeAccessTokenHandle, Action)

가장된 Windows ID로 지정된 작업을 실행합니다.Runs the specified action as the impersonated Windows identity. 가장된 메서드 호출을 사용하여 WindowsImpersonationContext에서 함수를 실행하지 않고 RunImpersonated(SafeAccessTokenHandle, Action)를 사용하여 매개 변수로 직접 함수를 제공할 수 있습니다.Instead of using an impersonated method call and running your function in WindowsImpersonationContext, you can use RunImpersonated(SafeAccessTokenHandle, Action) and provide your function directly as a 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

매개 변수

safeAccessTokenHandle
SafeAccessTokenHandle SafeAccessTokenHandle SafeAccessTokenHandle

가장 Windows ID의 SafeAccessTokenHandle입니다.The SafeAccessTokenHandle of the impersonated Windows identity.

action
Action Action Action

실행할 System.Action입니다.The System.Action to run.

예제

다음 예제에서는 사용 된 WindowsIdentity 사용자를 가장 하는 클래스입니다.The following example demonstrates the use of the WindowsIdentity class to impersonate a user.

경고

이 샘플 콘솔 화면에서 암호를 입력 하도록 사용자에 게 요청 합니다.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.

// 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);  
    }  
}  

RunImpersonated<T>(SafeAccessTokenHandle, Func<T>) RunImpersonated<T>(SafeAccessTokenHandle, Func<T>) RunImpersonated<T>(SafeAccessTokenHandle, Func<T>)

가장된 Windows ID로 지정된 함수를 실행합니다.Runs the specified function as the impersonated Windows identity. 가장된 메서드 호출을 사용하여 WindowsImpersonationContext에서 함수를 실행하지 않고 RunImpersonated(SafeAccessTokenHandle, Action)를 사용하여 매개 변수로 직접 함수를 제공할 수 있습니다.Instead of using an impersonated method call and running your function in WindowsImpersonationContext, you can use RunImpersonated(SafeAccessTokenHandle, Action) and provide your function directly as a 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

형식 매개 변수

T

함수에서 사용되고 반환되는 개체의 형식입니다.The type of object used by and returned by the function.

매개 변수

safeAccessTokenHandle
SafeAccessTokenHandle SafeAccessTokenHandle SafeAccessTokenHandle

가장 Windows ID의 SafeAccessTokenHandle입니다.The SafeAccessTokenHandle of the impersonated Windows identity.

func
Func<T>

실행할 System.Func입니다.The System.Func to run.

반환

T T T

함수의 결과입니다.The result of the function.

예제

다음 예제에서는 사용 된 WindowsIdentity 사용자를 가장 하는 클래스입니다.The following example demonstrates the use of the WindowsIdentity class to impersonate a user.

경고

이 샘플 콘솔 화면에서 암호를 입력 하도록 사용자에 게 요청 합니다.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.

// 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);  
    }  
}  

적용 대상