2013-03-29 44 views
0

我正在編寫一個應用程序,允許用戶管理Active Directory中的組。我想使用當前登錄用戶的身份,以便他們不必登錄到應用程序。該應用程序無需遠程解決問題,但在部署到測試服務器(Windows 2008R2 IIS6)之後,它試圖使用IIS APPPOOL身份而不是我的身份。IIS模擬返回應用程序池用戶

相關web.config設置:

<authentication mode="Windows"></authentication> 
<identity impersonate="true" /> 

代碼來檢查身份:

Session("Username") = ActiveDirectory.GetUsername(_ 
    WindowsIdentity.GetCurrent.User.Translate(GetType(NTAccount)).Value _ 
) 

ActiveDirectory中是一類的輔助功能,並且不包含任何模擬的邏輯。

如何獲取訪問應用程序的用戶的身份而不是IIS APPPOOL用戶的身份?

+0

什麼類型的用戶管理組.. ..?這是任何用戶..?如果是這樣'BAD IDEA'爲什麼你沒有一個AD管理組管理..?而另一件事情是,您仍然可以獲取機器的當前用戶並將其保留在不同的會話中,然後再解決您嘗試模擬的服務帳戶或帳戶用戶。 – MethodMan

+0

只有組管理員才能編輯成員資格。在解析到服務帳戶之前,我將如何訪問當前計算機的用戶? –

+0

'var currUser = System.Security.Principal.WindowsIdentity.GetCurrent()。Name.Split('\\');' – MethodMan

回答

1

我不確定這是否與您正在嘗試執行的操作相符。但是,檢索用戶,你會使用這樣的:

WindowsIdentity user = WindowsIdentity.GetCurrent(); 
WindowsPrincipal role = new WindowsPrincipal(user); 

與上述解決方案,你可以實際檢查的作用爲好,或者定義Identity.Name給您提供的信息。

另一種解決方案:

Environment.UserName; 
Environment.GetEnvironmentVariable("USERNAME"); 

現在我知道了WindowsIdentity將跨越拉,但其他人我不知道他們會如何處理的Active Directory。因此,您可能必須使用域上下文做些事情。

// Define Context 
PrincipalContext context = new PrincipalContext(ContextType.Domain); 

// Find User 
UserPrincipal user = UserPrincipal.Current; 

// Check User 
if (user != null) 
{ 
    string loginName = user.SamAccountName; // Whatever "Login Name Means" 
} 

這裏有很好的資源,以及:如果

Managing Directory Security:

MSDN Docs On Account Management

不知道那是你的意思到底是什麼,但希望幫助。

+0

感謝您回答Greg;不幸的是,環境變量返回與應用程序池或服務器相關的結果,而不是我的帳戶。基本上,我只是希望看到*誰*正在訪問該頁面,而無需再次登錄該站點,方法是查看誰正在登錄到他們正在使用的計算機上。 –

+1

@PhillipCopley是的,那就是我的想法。定義一個上下文可能是一個很好的選擇,然後就可能達到你所需要的。對不起,我無法提供更多的幫助。我會看看我能否爲你解決問題。 – Greg

1

我相信應用程序將以應用程序池的標識運行。爲了以不同身份運行,您需要模擬用戶。 MSDN有一篇關於它的文章。這是我根據幫助我完成類似事情的文章寫的一堂課。

class Impersonator : IDisposable 
    { 
     [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
     private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, 
      int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken); 

     [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
     private extern static bool CloseHandle(IntPtr handle); 

     WindowsIdentity newId; 
     WindowsImpersonationContext impersonatedUser; 
     private bool isDisposed; 

     public Impersonator(string user, string password, string domain) 
     { 
      SafeTokenHandle safeTokenHandle; 
      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. 
      bool returnValue = LogonUser(user, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle); 

      newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()); 
      impersonatedUser = newId.Impersonate(); 
     } 

     public void Dispose() 
     { 

      this.Dispose(true); 
      GC.SuppressFinalize(this); 
     } 

     public void Dispose(bool disposing) 
     { 
      if (!this.isDisposed) 
      { 
       if (disposing) 
       { 
        if (impersonatedUser != null) 
        { 
         this.impersonatedUser.Dispose(); 
        } 

        if (newId != null) 
        { 
         this.newId.Dispose(); 
        } 
       } 
      } 

      this.isDisposed = true; 
     } 

     ~Impersonator() 
     { 
      Dispose(false); 
     } 

     private sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid 
     { 
      private SafeTokenHandle() 
       : base(true) 
      { 
      } 

      [DllImport("kernel32.dll")] 
      [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] 
      [SuppressUnmanagedCodeSecurity] 
      [return: MarshalAs(UnmanagedType.Bool)] 
      private static extern bool CloseHandle(IntPtr handle); 

      protected override bool ReleaseHandle() 
      { 
       return CloseHandle(handle); 
      } 
     } 
    } 

基本上,當你想模仿用戶並在完成後處置類時,創建類的新實例。

我希望這有助於!

+0

感謝您對MisterXero的評論。這看起來像你要求的用戶名和密碼,是否正確?我正在尋找模擬登錄到任何計算機訪問此網站的用戶而無需再次登錄的用戶。 –

+0

在我的應用程序中,我有一個單獨的帳戶,可以訪問我用來更改身份的活動目錄(只有我知道用戶名和密碼)。我會通過System.Web.HttpContext.Current.User.Identity找到當前用戶。一旦我改變了應用程序的身份,並且知道用戶,我可以在AD中查找用戶以查看他們是否有權更改AD。如果他們這樣做,那麼無論你需要用AD做什麼,都可以繼續。 – MisterXero