【问题标题】:C# PrincipalContext only changes password for some users, not allC# PrincipalContext 仅更改某些用户的密码,而不是全部
【发布时间】:2023-04-11 07:37:01
【问题描述】:

我正在尝试更改系统中所有成员的 AD 密码,但我的代码仅成功更改了某些成员的密码。对于不能修改密码的会员,提示如下错误:

System.NullReferenceException:对象引用未设置为对象的实例。在 C:\Users\Intern\source\repos\changep1\changep1\changep2.aspx.cs:line 52 中的 changep1.changep2.changeUserPassword(String _userID, String _oldPassword, String _newPassword) 处

这是我的 C# 代码:

 public string changeUserPassword(string _userID, string _oldPassword, string _newPassword)
        {
            string message="";
            try
            {
                PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, "extra.sales-comm.local", "DC=sales-comm,DC=local",
                ContextOptions.SimpleBind, @"admin", "Passw@rd");
                UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, _userID);

                oUserPrincipal.ChangePassword(_oldPassword, _newPassword);


                oUserPrincipal.Save();
            }
            catch (Exception e)
            {
                message = e.ToString();
            }
            return message;
        }

我不明白为什么我的代码不会更改所有 AD 成员的密码。请帮忙谢谢。

【问题讨论】:

    标签: c# active-directory ldap change-password principalcontext


    【解决方案1】:

    您的代码需要在查找身份时检查 UserPrincipal 值是否为空,以防找不到传递的用户 ID。您没有在代码中检查相同的内容。这看起来像是它给出空指针异常的原因。

    阅读方法UserPrincipal.FindByIdentity Method (PrincipalContext, String)的文档:

    返回与指定身份匹配的用户主体对象 价值。

    参数

    上下文类型:System.DirectoryServices.AccountManagement.PrincipalContext

    指定服务器或域的 PrincipalContext 执行操作。

    identityValue 类型:System.String

    用户主体的身份。 此参数可以是 IdentityType 中包含的任何格式 枚举。


    ... // IdentityType 枚举成员如下:

    会员名----------- 说明

    DistinguishedName-------------- 身份是 专有名称 (DN)。

    Guid ---------------------- 身份是一个全局唯一标识符 (GUID)。

    Name ---------------- 身份是一个名字。

    SamAccountName--------------- 身份是安全帐户管理器 (SAM) 名称。

    Sid ------------- 身份是Security中的一个安全标识符(SID) 描述符定义语言 (SDDL) 格式。

    UserPrincipalName 身份是用户主体名称 (UPN)。

    如下图所示:

             try
                {
                    PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, "extra.sales-comm.local", "DC=sales-comm,DC=local", ContextOptions.SimpleBind, @"admin", "Passw@rd");
                    UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, _userID);
                   if ( null != oUserPrincipal){
                    oUserPrincipal.ChangePassword(_oldPassword, _newPassword);    
                    oUserPrincipal.Save();
                   }
                   else {
                   // return the message that the user-id could not be found.
                   // preferably passed argumnet in user-id should be **SamAccountName**
                   // please make sure that the user-id corresponds to the members mentioned above
                   }
                }
                catch (Exception e)
                {
                    message = e.ToString();
                }
    

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 1970-01-01
      • 2012-07-25
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      相关资源
      最近更新 更多