2012-10-01 131 views
7

以下代碼已運行3個月,沒有任何問題。 由於今天我收到以下錯誤; 「調用的目標引發異常」 和內部異常; 。 「訪問被從HRESULT拒絕(例外:0X80070005(E_ACCESSDENIED)」LDAP SetPassword訪問被拒絕

認證功能在這裏工作是我的職能;

public bool Authenticate(string strUserName, string strPassword) 
    { 
     bool authenticated = false; 
     using (
       var entry = new DirectoryEntry("LDAP://myldapserver", strUserName + "@domain", strPassword, 
               AuthenticationTypes.Secure)) 
     { 
      try 
      { 
       object nativeObject = entry.NativeObject; 
       authenticated = true; 
      } 
      catch (DirectoryServicesCOMException ex) 
      { 
       return false; 
      } 

     } 
     return authenticated; 
    } 

而且ChangePassword方法;

public bool ChangePassword(string strUserName, string strOldPassword, string strNewPassword) 
    { 
     const long ADS_OPTION_PASSWORD_PORTNUMBER = 6; 
     const long ADS_OPTION_PASSWORD_METHOD = 7; 
     const int ADS_PASSWORD_ENCODE_REQUIRE_SSL = 0; 
     const int ADS_PASSWORD_ENCODE_CLEAR = 1; 
     string strPort = "636"; 
     int intPort; 
     intPort = Int32.Parse(strPort); 

     try 
     { 
      string strUserString = "domain" + @"\" + strUserName.Trim(); 

      var entry = new DirectoryEntry("LDAP://myldapserver", strUserString, strOldPassword, 
              AuthenticationTypes.Secure); 
      var search = new DirectorySearcher(entry); 
      string strFilter = "(SAMAccountName=" + strUserName + ")"; 
      search.Filter = strFilter; 
      SearchResult result = search.FindOne(); 
      DirectoryEntry user = result.GetDirectoryEntry(); 

      user.Invoke("SetOption", new object[] { ADS_OPTION_PASSWORD_PORTNUMBER, intPort }); 
      user.Invoke("SetOption", new object[] { ADS_OPTION_PASSWORD_METHOD, ADS_PASSWORD_ENCODE_CLEAR }); 
      **user.Invoke("SetPassword", new object[] { strNewPassword });** 
      user.CommitChanges(); 
      user.Close(); 
     } 

     catch (Exception exception) 
     { 
      string msg = exception.InnerException.Message; 
      return false; 
     } 
     return true; 
    } 

它當我調用SetPassword屬性時拋出expcetion 任何幫助將不勝感激

+0

哪條線路故障?什麼方法失敗? –

+1

ChangePassword是失敗的用戶user.Invoke(「SetPassword」,新對象[] {strNewPassword}); – user1595357

+0

您在頂部發布的消息是內部異常?我問,因爲內部異常包含實際收到的COM異常。 –

回答

1

這裏是th E實施例: -

PrincipalContext pr = new PrincipalContext(ContextType.Domain, "corp.local", "OU=" + OU + ",OU=Users,dc=corp,dc=local", username, password); 
UserPrincipal us = new UserPrincipal(pr); 

更改密碼

user.SetPassword("setPassword"); 

如果你希望用戶應在下次登錄時更改密碼,你可以用這樣的。

user.ExpirePasswordNow(); 

這裏是你的全碼: -

public static Boolean ResetPassword(string username, string password, string DomainId, string setpassword, Boolean UnlockAccount,Boolean NextLogon) 
{ 
    PrincipalContext pr = new PrincipalContext(ContextType.Domain, "corp.local", "dc=corp,dc=local", username, password); 
    UserPrincipal user = UserPrincipal.FindByIdentity(pr, DomainId); 

    Boolean flag = false; 
    if (user != null && user.Enabled == true) 
    { 
     if (UnlockAccount) 
     { 
      user.UnlockAccount(); 
     } 
     user.SetPassword(setpassword); 
     if (NextLogon) 
     { 
      user.ExpirePasswordNow(); 
     } 
     user.Save(); 
     flag = true; 
    } 
    else 
     { 
     flag = false; 
     } 
    user.Dispose(); 
    pr.Dispose(); 
    return flag; 
    } 

我找到了一個很好的文章在這裏,如果你想在你的方式來使用它,在這裏一看,

http://www.primaryobjects.com/cms/article66.aspx

+0

謝謝,親切的先生 – user1595357

+0

它對你有效嗎? – RL89

+0

我試圖讓它工作,將發佈結果 – user1595357