2017-10-13 384 views
0

在asp.net MVC應用程序中,當使用directoryEntry.Invoke嘗試重置密碼時,遇到訪問被拒絕錯誤。DirectoryEntry SetPassword方法返回拒絕訪問異常

用戶試圖更改他/她的密碼訪問該頁面,並且在IIS中標記了SSL requiredClient Certificates - Required

相關的代碼:

directoryEntry.Invoke("SetPassword", new object[] { model.Password }); 
       directoryEntry.Properties["LockOutTime"].Value = 0; 
       directoryEntry.Close(); 

確切的錯誤是 -

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) 
    --- End of inner exception stack trace --- 
    at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) 

的Web.config -

<authentication mode="Windows" /> 
    <identity impersonate="false" /> 
    <authorization> 
     <deny users="?" /> 
    </authorization> 
  • 該應用池是一個廣告帳戶下運行;也是當地的一部分 管理組[Domain1\AppPoolUser]
  • 應用程序請求用戶證書
  • 嘗試更改密碼[Domain2\testUser]的用戶以及運行應用程序池的帳戶位於不同的域中,但這不可能是問題。對於AppPoolUser的有效權限允許testUser帳戶上的ChangePassword。
  • 我甚至嘗試在與 測試帳戶相同的用戶帳戶下運行應用程序池,但它不會改變任何內容。

已經在網上查過,但是不清楚這個問題可能是什麼。我看到的最相關的事情是這樣的 - Setting ASP.Net Permissions - Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

但是,正如我所提到的,應用程序池在有限的技術帳戶下運行,我認爲SSL證書沒有任何問題。

  1. 我是否需要爲AD中的應用程序池帳戶請求控制權委派?
  2. 或者有可能是我錯過了另一個問題。

回答

0

我們有類似的要求來更改或重置密碼。我們使用下面的代碼片段。

 /// <summary> 
     /// Resets the user password. 
     /// </summary>   
     public static void ResetUserPassword(string domain, string domainUsername, string domainPassword, string sAMAccountName, string newPassword, 
      bool askToChangePassword, bool unlockAccount, bool passRespectDomainPolicy, bool superuser) 
     { 
      // Get root directory entry 
      using (var entry = GetDirectoryEntry(domain, domainUsername, domainPassword, AuthenticationTypes.Secure)) 
      { 
       var displayName = string.Empty; 

       // Search for the user with the same sAMAccountName 
       using (var searcher = new DirectorySearcher(entry)) 
       { 
        // Filter results by SAMAccountName 
        searcher.Filter = string.Format("(SAMAccountName={0})", sAMAccountName); 

        // Search and return only one result 
        var result = searcher.FindOne(); 

        // Check if result is returned 
        if (result == null) throw new Exception("Could not find user: " + sAMAccountName); 

        // Get the user directory entry 
        var userEntry = result.GetDirectoryEntry(); 

        // Read name value 
        if (userEntry.Properties.Contains("displayName") && userEntry.Properties["displayName"].Count > 0) 
         displayName = Convert.ToString(userEntry.Properties["displayName"][0]); 

        // Validate password 
        // string errorMessage; 
        // if (passRespectDomainPolicy && 
        // !IsValidPassword(domain, sAMAccountName, newPassword, displayName, userEntry, superuser, out errorMessage)) 
        // { 
         // if (!string.IsNullOrEmpty(errorMessage)) throw new Exception(errorMessage); 
         // throw new Exception("Password is not valid as per AD policy. Please consult Administrator."); 
        // } 

        // Earlier we used impersonation to reset password on same DC. 
        // But that didn't worked and so removed. 
        userEntry.Invoke("SetPassword", newPassword); 

        // 0(for on) and -1(for off) for LDAP case. For WinNT it is opposite. 
        // Set "Ask to change password at next login" 
        if (askToChangePassword) 
         userEntry.Properties["pwdLastSet"].Value = 0; 

        // Unlock account if required 
        if (unlockAccount) 
         userEntry.Properties["lockoutTime"].Value = 0; 

        // Commit changes 
        userEntry.CommitChanges(); 
       } 
      } 
     } 

不可忽視的一點是,我們正在運行的代碼userEntry.Invoke("SetPassword", newPassword);根目錄條目using (var entry = GetDirectoryEntry(domain, domainUsername, domainPassword, AuthenticationTypes.Secure)){的上下文中。

我的意思是entry代表持有域'管理員用戶和密碼的對象。此管理員用戶必須具有完全權限才能在AD中進行更改。

讓我們知道您的測試結果。

相關問題