2012-01-26 11 views
0

動的にasp.netでユーザーを偽装する方法はありますか?偽装されたユーザーが毎回異なる可能性があるため、各リクエストのコンテキスト内で偽装を行う必要があります。このため、すべての要求に適用されるweb.configは使用できません。asp.netの動的偽装

答えて

2

私はこのクラスがどこにあるのか覚えていません。しかし、これはあなたのためにうまくいくはずです。

using System; 
using System.Security.Principal; 
using System.Runtime.InteropServices; 

public class Impersonation 
    { 
     public static int LOGON32_LOGON_INTERACTIVE = 2; 
     public static int LOGON32_PROVIDER_DEFAULT = 0; 

     [DllImport("advapi32.dll")] 
     public static extern int LogonUserA(string lpxzUsername, string lpzDomain, string lpzPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 
     [DllImport("advapi32.dll")] 
     public static extern int DuplicateToken(IntPtr ExistingTokenHandle, int ImpersonationLevel, ref IntPtr DuplicateTokenHandle); 
     [DllImport("advapi32.dll")] 
     public static extern long RevertToSelf(); 

     [DllImport("Kernel32.dll")] 
     public static extern long CloseHandle(IntPtr handle); 

     public static WindowsImpersonationContext impersonationContext; 

     public static bool impersonateValidUser(string userName, string domain, string password) 
     { 
      WindowsIdentity tempWindowsIdentity; 
      IntPtr token = IntPtr.Zero; 
      IntPtr tokenDuplicate = IntPtr.Zero; 
      bool ValidUser = false; 

      if (RevertToSelf() != 0) 
      { 
       if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) 
       { 
        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
        { 
         tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); 
         impersonationContext = tempWindowsIdentity.Impersonate(); 
         if (impersonationContext != null) 
         { 
          ValidUser = true; 
         } 
        } 
       } 
      } 

      if (!tokenDuplicate.Equals(IntPtr.Zero)) 
      { 
       CloseHandle(tokenDuplicate); 
      } 
      if (!token.Equals(IntPtr.Zero)) 
      { 
       CloseHandle(token); 
      } 
      return ValidUser; 

     } 

     public static void undoImpersonation() 
     { 
      try 
      { 
       impersonationContext.Undo(); 
      } 
      catch 
      { 
      } 
     } 
    } 

その後、あなただけの

Impersonation.impersonateValidUser("user", "domain", "password"); 

のようにそれを呼び出すには、それがお役に立てば幸いです。