ActiveDirectory更新用户获取UnauthorizedAccessException

时间:2016-08-12 20:57:21

标签: c# directoryservices userprincipal

当我尝试更新域控制器上的用户描述时,我收到错误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
        }
     }

Common.DCData是一个从数据库中获取数据的简单类。我已经仔细检查了数据库中的用户名和密码

0 个答案:

没有答案