C#PrincipalContext仅更改某些用户的密码,而不是全部用户的密码 [英] C# PrincipalContext only changes password for some users, not all

查看:525
本文介绍了C#PrincipalContext仅更改某些用户的密码,而不是全部用户的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改系统中所有成员的AD密码,但是我的代码仅成功更改了某些成员的密码.对于无法更改密码的成员,它将显示以下错误:

I'm trying to change the AD password of all members in my system, but my code only changes the password for some members successfully. For members whose password can't be changed, it shows the following error:

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

System.NullReferenceException: Object reference not set to an instance of an object. at changep1.changep2.changeUserPassword(String _userID, String _oldPassword, String _newPassword) in C:\Users\Intern\source\repos\changep1\changep1\changep2.aspx.cs:line 52

这是我的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成员的密码.请帮助谢谢.

I don't understand why my code doesn't change passwords for all AD members. Please help thanks.

推荐答案

您的代码需要在查找身份时检查UserPrincipal值是否为null,以防找不到传递的用户ID.您尚未在代码中检查相同的内容.这看起来像是它给出了Null Pointer Exception的原因.

Your code needs to check if the UserPrincipal value while finding the identity is null, in case if the passed user-id is not found. You've not checked the same in your code. This looks like the reason why it is giving a Null Pointer Exception.

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

返回与指定标识匹配的用户主体对象 值.

Returns a user principal object that matches the specified identity value.

参数

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

PrincipalContext,它指定要针对的服务器或域 操作已执行.

The PrincipalContext that specifies the server or domain against which operations are performed.

identityValue类型:System.String

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

The identity of the user principal. This parameter can be any format that is contained in the IdentityType enumeration.


...////下面列出了IdentityType枚举成员:

... // IdentityType Enumeration members are listed below:

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

Member name----------------------- Description

DistinguishedName --------------身份是一个 专有名称(DN).

DistinguishedName-------------- The identity is a Distinguished Name (DN).

向导----------------------身份是全球唯一标识符(GUID).

Guid ---------------------- The identity is a Globally Unique Identifier (GUID).

名称----------------身份就是名称.

Name ---------------- The identity is a name.

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

SamAccountName--------------- The identity is a Security Account Manager (SAM) name.

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

Sid ------------- The identity is a Security Identifier (SID) in Security Descriptor Definition Language (SDDL) format.

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

UserPrincipalName The identity is a User Principal Name (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();
            }

这篇关于C#PrincipalContext仅更改某些用户的密码,而不是全部用户的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆