2015-01-09 146 views
0

我是一個ASP.NET新手,並創建一個內部應用程序(將由組內的10個用戶使用)。 其中一個目標是將用戶添加到Active Directory組。出於審計目的,應用程序用戶應該使用自己的憑據進行AD更改(而不是爲此唯一目的而創建的通用標識)。該網站託管在Windows Server 2008 R2/IIS 7.0上,並且所有內容都位於企業域中。ASP.NET與活動目錄:用戶憑證

IIS設置:
應用程序池名稱:UserAppMapping,集成,ApplicationPoolIdentity

驗證:ASP.NET模擬和Windows身份驗證只被啓用。

VB.NET代碼(我已刪除了所有的錯誤檢查,這樣我就可以看到實際的錯誤):

Public Sub AddUserToGroup(userId As String, groupName As String, Optional GroupDN As String = "") 

     Using (HostingEnvironment.Impersonate()) 

      Dim domainName As String = "DOM" 

      Dim ou = [String].Format(CultureInfo.InvariantCulture, "dc={0},dc=myorg,dc=com", domainName) 

      Dim domain = [String].Format(CultureInfo.InvariantCulture, "{0}.myorg.com", domainName) 

      Using insPrincipalContext = New PrincipalContext(ContextType.Domain, domain, ou) 
       If insPrincipalContext IsNot Nothing Then 
        Dim insGroupPrincipal As GroupPrincipal = GroupPrincipal.FindByIdentity(insPrincipalContext, System.DirectoryServices.AccountManagement.IdentityType.Name, groupName) 

        Dim user As UserPrincipal = UserPrincipal.FindByIdentity(insPrincipalContext, userId) 

        If insGroupPrincipal IsNot Nothing Then 
         'add user to group 
         If user.IsMemberOf(insGroupPrincipal) Then 
          LabelErr.Text = userId + " is already part of " + groupName 
         Else 
          insGroupPrincipal.Members.Add(user) 
          insGroupPrincipal.Save() 
          Label1.Text = "User added successfully" 
         End If 
        Else 
         Label1.Text = "Cannot retrieve information about " + groupName + " from Active Directory" 
        End If 
       Else 
        Label1.Text = "Principal Context is Null" 
       End If 
       insPrincipalContext.Dispose() 
      End Using 
     End Using 
    End Sub 

`

問題

一切工作正常,但當在代碼中遇到以下行時:

insGroupPri nipal.Members.Add(用戶)
insGroupPrincipal.Save()

應用程序提示輸入用戶憑證。雖然我瞭解此功能中的代碼正在Windows Identity「IIS APPPOOL \ UserAppMapping」的上下文中運行,但它會提示輸入有權訪問Active Directory的憑據。 是否有應用程序用戶修改AD組(我假設用戶可以直接在AD中修改組),而無需鍵入額外的憑據?

如果我從代碼中刪除「使用(HostingEnvironment.Impersonate())」,它會在遇到GroupPrincipal.FindByIdentity時立即失敗。

web.config中設置有:

身份驗證模式= 「窗口」
身份冒充= 「真」

在此先感謝。

回答

0

請執行以下操作。

  1. 確保IIS身份驗證設置爲Windows Auth = enabled,Anonymous = false。除了web.config之外,這是必需的。

第2步:您需要的帳戶在AD中的權限高於ApplicationPoolIndentity。

  1. 您需要一個網絡帳戶才能執行AD工作。創建一個新的網絡帳戶,然後在IIS的應用程序池中將其從ApplicationPoolIndentity更改爲自定義帳戶。然後輸入您的新AD帳戶和密碼。(我建議讓AD帳號密碼永不過期)
+0

IIS身份驗證已設置爲Windows身份驗證和ASP.NET模擬。匿名設置爲False。如果我創建一個新的網絡帳戶並設置爲應用程序池標識,那麼不應該該帳戶執行AD中的所有寫入操作嗎?那麼我將如何知道哪個應用程序用戶實際進行了更改? – FromNY 2015-01-11 15:41:02