ActiveDirectory更新获取UnauthorizedAccessException的用户
本文关键字:用户 UnauthorizedAccessException 获取 更新 ActiveDirectory | 更新日期: 2023-09-27 18:02:09
当我尝试在域控制器上更新用户的描述时,我得到错误UnauthorizedAccessException。我已经验证了我使用的用户名和密码实际上能够更新用户。我以服务用户名和密码登录,手动修改数据。这是我的代码。
private PrincipalContext pc = new PrincipalContext(ContextType.Domain, Common.DCData.serverName, null, ContextOptions.Negotiate, Common.DCData.userName, Common.DCData.password );
public bool Save(UserData data)
{
try
{
UserPrincipal up = UserPrincipal.FindByIdentity(pc, data.userName);
if ((data.DisplayName != null) && (data.DisplayName != "") && (data.DisplayName != up.DisplayName))
up.DisplayName = data.DisplayName;
if ((data.givenName != null) && (data.givenName != "") && (data.givenName != up.GivenName))
up.GivenName = data.givenName;
if ((data.middleName != null) && (data.middleName != "") && (data.middleName != up.MiddleName))
up.MiddleName = data.middleName;
if ((data.surname != null) && (data.surname != "") && (data.surname != up.Surname))
up.Surname = data.surname;
if ((data.emailAddress != null) && (data.emailAddress != "") && (data.emailAddress != up.EmailAddress))
up.EmailAddress = data.emailAddress;
if ((data.voiceTelephoneNumber != null) && (data.voiceTelephoneNumber != "") && (data.voiceTelephoneNumber != up.VoiceTelephoneNumber))
up.VoiceTelephoneNumber = data.voiceTelephoneNumber;
if ((data.description != null) && (data.description != "") && (data.description != up.Description))
up.Description = data.description;
up.Save(pc);
return true;
}
catch(Exception ex)
{
// Some logging goes here
}
}
常见。DCData是一个从数据库获取数据的简单类。我已经仔细检查了数据库中的用户名和密码
在引用静态成员时,我为从SQL中提取数据而创建的静态类没有自动实例化(正如我认为的那样)。我最终使类成为一个非静态类,并在使用它之前手动实例化类。