0

当我尝试更新域控制器上的用户描述时,我收到错误 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 是一个从数据库获取数据的简单类。我已经仔细检查了数据库中的用户名和密码

4

1 回答 1

0

我创建的用于从 SQL 中提取数据的静态类在引用静态成员时没有自动实例化(正如我所想的那样)。我最终使该类成为非静态类,并在使用之前手动实例化该类。

于 2016-08-15T16:17:36.527 回答