在密码更改活动目录用户错误错误、密码、目录、用户

2023-09-08 12:51:42 作者:有很多事情你当时想不通

你好我想重置的Active Directory用户密码,但我得到错误,以下是我的code:

 公共字符串的ChangePassword(字符串标识,串OldPassword更改,串密码)
 {
      字符串的成功=成功;
      尝试
      {


          的DirectoryEntry UserEntry = NULL;
          的DirectoryEntry条目=新的DirectoryEntry(LDAP://.../DC=Domain,DC=COM,身份,OldPassword更改);

          DirectorySearcher从搜索=新DirectorySearcher从(输入);
          信息搜索结果resultsearch = search.FindOne();
          如果(resultsearch == NULL)
          {
              成功=找不到用户在这一领域;
          }
          其他
          {

              成功=发现;
              UserEntry = resultsearch.GetDirectoryEntry();
              UserEntry.Username = @域\管理员;
              UserEntry.Password =密码;
              UserEntry.AuthenticationType = AuthenticationTypes.None;

              如果(UserEntry == NULL)
                  成功=找不到用户在这一领域;
              其他
              {
                  尝试
                  {
                      成功= UserEntry.Username.ToString();


    UserEntry.Invoke(的ChangePassword,新的对象[] {OldPassword更改,密码});
                      UserEntry.CommitChanges();

                  }
                  赶上(例外前)
                  {
                      成功= ex.ToString();
                  }
              }
          }
      }
      赶上(例外前)
      {
          成功= ex.ToString();
      }
 

所以,我收到错误的 UserEntry.Invoke(的ChangePassword,新的对象[] {OldPassword更改,密码});                           UserEntry.CommitChanges();

错误:

  System.Runtime.InteropServices.COMException(0x80020006):未知的名称。 (从HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME))
        在System.DirectoryServices.DirectoryEntry.Invoke(字符串方法名,对象[]参数)
        在WebService.ChangePassword(字符串标识,字符串OldPassword更改,字符串密码)在C:\的Inetpub \ wwwroot的\的WebSite1 \ APP_ code \ WebService.cs:行370
 

如果您正在使用的.NET Framework 3.5或更高版本解决方案

中,code以下就能解决问题。类的定义中省略。

 使用System.DirectoryServices.AccountManagement;

公共静态字符串的ChangePassword(字符串管理用户,串ADMINPASSWORD,
    字符串域,串容器,用户名字符串,字符串NEWPASSWORD)
{
    尝试
    {
        PrincipalContext principalContext =
            新PrincipalContext(ContextType.Domain,域,容器,
                管理用户,ADMINPASSWORD);
        UserPrincipal用户= UserPrincipal.FindByIdentity(principalContext,用户名);
        如果(用户== NULL)返回找不到用户在这一领域;

        user.SetPassword(新密码);
        返回user.Name;
    }
    赶上(例外前)
    {
        返回ex.Message;
    }
}
 
电子物证 CSDN博客

用法:

 的ChangePassword(@域\管理员,密码,域,
  DC =域,DC = COM,用户名,新密码);
 

编辑:增加了一个版本的.NET 2.0框架

一个修改密码的方法.NET 2.0:

 公共静态字符串ChangePassword20(字符串管理用户,串ADMINPASSWORD,
    字符串容器,字符串domainController,用户名字符串,字符串NEWPASSWORD)
{
    常量AuthenticationTypes authenticationTypes = AuthenticationTypes.Secure |
        AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind;

    的DirectoryEntry searchRoot = NULL;
    DirectorySearcher从搜索= NULL;
    的DirectoryEntry userEntry = NULL;

    尝试
    {
        sea​​rchRoot =新的DirectoryEntry(的String.Format(LDAP:// {0} / {1},
            domainController,集装箱)
            管理用户,ADMINPASSWORD,authenticationTypes);

        搜索=新DirectorySearcher从(searchRoot);
        sea​​rcher.Filter =的String.Format(sAMAccountName赋= {0},用户名);
        sea​​rcher.SearchScope = SearchScope.Subtree;
        sea​​rcher.CacheResults = FALSE;

        信息搜索结果信息搜索结果= searcher.FindOne(); ;
        如果(信息搜索结果== NULL)返回找不到用户在这一领域;

        userEntry = searchResult.GetDirectoryEntry();

        userEntry.Invoke(SetPassword,新的对象[] {新密码});
        userEntry.CommitChanges();

        回到新密码设置;
    }
    赶上(例外前)
    {
        返回ex.ToString();
    }
    最后
    {
        如果(!userEntry = NULL)u​​serEntry.Dispose();
        如果(!搜索= NULL)searcher.Dispose();
        如果(searchRoot!= NULL)searchRoot.Dispose();
    }
}
 

用法:

  ChangePassword20(@域\管理员,密码,DC =域,DC = COM,
    domainControllerName,username的,新密码);
 

Hi I am trying to reset password of Active Directory User But I Am getting error,Following is my Code:

    public string ChangePassword(string Identity,string OldPassword, string Password)
 {
      string success = "Success";
      try
      {


          DirectoryEntry UserEntry = null;
          DirectoryEntry entry = new DirectoryEntry("LDAP://.../DC=Domain,DC=COM", Identity, OldPassword);

          DirectorySearcher search = new DirectorySearcher(entry);
          SearchResult resultsearch = search.FindOne();
          if (resultsearch == null)
          {
              success = "User Not Found In This Domain";
          }
          else
          {

              success = "find";
              UserEntry = resultsearch.GetDirectoryEntry();
              UserEntry.Username = @"Domain\Administrator";
              UserEntry.Password = "password";
              UserEntry.AuthenticationType = AuthenticationTypes.None;

              if (UserEntry == null)
                  success = "User Not Found In This Domain";
              else
              {
                  try
                  {
                      success = UserEntry.Username.ToString();


    UserEntry.Invoke("ChangePassword", new object[] { OldPassword, Password });
                      UserEntry.CommitChanges();

                  }
                  catch (Exception ex)
                  {
                      success = ex.ToString();
                  }
              }
          }
      }
      catch (Exception ex)
      {
          success = ex.ToString();
      }

So I am getting Error in UserEntry.Invoke("ChangePassword", new object[] { OldPassword, Password }); UserEntry.CommitChanges();

Error:

        System.Runtime.InteropServices.COMException (0x80020006): Unknown name.           (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
        at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)
        at WebService.ChangePassword(String Identity, String OldPassword, String Password) in c:\inetpub\wwwroot\WebSite1\App_Code\WebService.cs:line 370

解决方案

If you are using .NET Framework 3.5 or later, the code below will solve the problem. Class definition is omitted.

using System.DirectoryServices.AccountManagement;

public static string ChangePassword(string adminUser, string adminPassword,
    string domain, string container, string userName, string newPassword)
{
    try
    {
        PrincipalContext principalContext = 
            new PrincipalContext(ContextType.Domain, domain, container, 
                adminUser, adminPassword);
        UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, userName);
        if (user == null) return "User Not Found In This Domain";

        user.SetPassword(newPassword);
        return user.Name;
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

Usage:

ChangePassword(@"DOMAIN\Administrator", "password", "DOMAIN",
  "DC=Domain,DC=COM", userName, newPassword);

EDIT: Added a version for .NET 2.0 framework.

A change password method for .NET 2.0:

public static string ChangePassword20(string adminUser, string adminPassword,
    string container, string domainController, string userName, string newPassword)
{
    const AuthenticationTypes authenticationTypes = AuthenticationTypes.Secure |
        AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind;

    DirectoryEntry searchRoot = null;
    DirectorySearcher searcher = null;
    DirectoryEntry userEntry = null;

    try
    {
        searchRoot = new DirectoryEntry(String.Format("LDAP://{0}/{1}", 
            domainController, container), 
            adminUser, adminPassword, authenticationTypes);

        searcher = new DirectorySearcher(searchRoot);
        searcher.Filter = String.Format("sAMAccountName={0}", userName);
        searcher.SearchScope = SearchScope.Subtree;
        searcher.CacheResults = false;

        SearchResult searchResult = searcher.FindOne(); ;
        if (searchResult == null) return "User Not Found In This Domain";

        userEntry = searchResult.GetDirectoryEntry();

        userEntry.Invoke("SetPassword", new object[] { newPassword });
        userEntry.CommitChanges();

        return "New password set";
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
    finally
    {
        if (userEntry != null) userEntry.Dispose();
        if (searcher != null) searcher.Dispose();
        if (searchRoot != null) searchRoot.Dispose();
    }
}

Usage:

ChangePassword20(@"DOMAIN\Administrator", "password", "DC=Domain,DC=COM",
    "domainControllerName", "userName", "newPassword");