2009-12-02 75 views
1

我有一個自定義成員資格/角色提供程序,由於項目的性質,它需要管理員以用戶身份登錄,同時幫助他們進行查詢。使用ASP.NET成員資格提供程序模仿

現在,很容易用選定的成員帳戶重新登錄管理員,但這意味着管理員將被有效註銷。我正在尋找一種方法來允許管理員模擬用戶,但很容易隨時切換回自己的帳戶。

有什麼建議嗎?

回答

0

這應該是你想要的東西。

您可以使用所需域帳戶的用戶名和密碼來調用ImpersonateValidUser方法。然後在註銷時將其反轉。

你應該能夠彎曲這與您的自定義會員提供商一起工作。

// Constants for impersonation 
private WindowsImpersonationContext impersonationContext; 
public const int LOGON32_LOGON_INTERACTIVE = 2; 
public const int LOGON32_PROVIDER_DEFAULT = 0; 

/// <summary> 
/// Changes the account we are running under. 
/// </summary> 
/// <param name="username">Username of a local admin account</param> 
/// <param name="domain">Domain of the username</param> 
/// <param name="password">Password of a local admin account</param> 
/// <returns></returns> 
private bool ImpersonateValidUser(String username, String domain, String password) 
{ 
    WindowsIdentity tempWindowsIdentity; 
    IntPtr token = IntPtr.Zero; 
    IntPtr tokenDuplicate = IntPtr.Zero; 

    if (RevertToSelf()) 
    { 
     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) 
       { 
        CloseHandle(token); 
        CloseHandle(tokenDuplicate); 
        return true; 
       } 
      } 
     } 
    } 
    if (token != IntPtr.Zero) 
     CloseHandle(token); 
    if (tokenDuplicate != IntPtr.Zero) 
     CloseHandle(tokenDuplicate); 
    return false; 
} 

/// <summary> 
/// Cancel the impersonation and revent the thread to the 
/// default account. Typically DOMAIN\NETWORK_SERVICE or similar. 
/// </summary> 
private void UndoImpersonation() 
{ 
    impersonationContext.Undo(); 
} 
+0

看起來像這與AD帳戶有很大關係 - http://support.microsoft.com/kb/306158:我不認爲它可以幫助我。 – LiamB 2009-12-03 09:29:01

相關問題