2014-07-22 97 views
0

我想在AD創建一個新用戶。我使用以下代碼:調用的目標引發了異常。引發UserPrincipal

 using (var context = new PrincipalContext(ContextType.Domain, this.DomainName, userWithWMIAccessRights, userWithWMIAccessrightsPassword)) 
     { 
      UserPrincipal user = new UserPrincipal(context); 
      user.DisplayName = Newusername; 
      user.Name = Newusername; 
      user.Enabled = true; 
      user.SetPassword(passwordOfThenewUser); 
      user.Save(); //<-- throws exception InnerException = {"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"} 
      return user; 
     } 

我設置的userWithWMIAccessrightsPassword那樣:

  • userWithWMIAccessrightsPassword是Enterpirse管理員,域管理員和域用戶
  • 的構件冉從命令提示DCOMCNFG 。
  • 從服務列表中選擇Windows Management Instrumentation。
  • 通過從安全選項卡添加userWithWMIAccessrightsPassword帳戶更新了自定義權限,並授予他完全的權限。

該web應用程序與AD運行在同一臺機器上。當我使用Visual Studio調試Web應用程序時,用戶和密碼設置沒有問題。

當在AD機器上部署Web應用程序時,創建用戶的函數會在設置密碼的代碼行中拋出{「Access is denied。(Exception from HRESULT:0x80070005(E_ACCESSDENIED))」}。用戶被創建並且沒有被啓用並且沒有密碼。

當PrincipalContext使用完全權限的AD用戶時,爲什麼我會得到Access拒絕?

回答

0

好吧,似乎PrincipalContext是!= impersionating。

我可以解決這個問題有:

1)設置PrincipalContext的容器,並調用ValidateCredentials(this.UserName,this.Password,ContextOptions.Negotiate)。

2)

using (new Impersonator(this.UserName, this.DomainName, this.Password, LogonType.LOGON32_LOGON_SERVICE, LogonProvider.LOGON32_PROVIDER_WINNT40)) 
     { 
      using (var context = CreatePrincipalContext()) 
      { 
       UserPrincipal user = new UserPrincipal(context); 
       user.DisplayName = username; 
       user.Name = username; 
       user.Enabled = true; 
       user.SetPassword(password); 
       user.Save(); 
       return user; 
      } 
     } 

但我還是感到困惑,需要與Principalcontext冒領類的。我認爲PrincipalContext取代了impersionate類。

相關問題