7

次のコードは、3 か月間問題なく動作していました。今日から、次のエラーが発生しています。「呼び出しのターゲットによって例外がスローされました」と内部例外。「アクセスが拒否されました。(HRESULT からの例外: 0x80070005 (E_ACCESSDENIED)」

認証機能が動作します。これが私の機能です。

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 プロパティを呼び出すと、expceion がスローされます。どんな助けでも大歓迎です。

4

1 に答える 1

2

ここに例があります: -

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

于 2012-10-01T12:33:48.697 に答える