.net UserPrincipal.Save()上的未授权访问异常

.net UserPrincipal.Save()上的未授权访问异常,.net,active-directory,.net,Active Directory,我正在运行以下命令 static void UpdateEmployeeID(string userName, string id) { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); bool val = ctx.ValidateCredentials("myusername", "mypassword"); UserPrincipal user = U

我正在运行以下命令

  static void UpdateEmployeeID(string userName, string id)
    {
       PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        bool val = ctx.ValidateCredentials("myusername", "mypassword");

        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
        if (user != null)
        {
            user.EmployeeId = id;
            user.Save(ctx);
        }
     }
当它运行时,我在User.Save(ctx)上收到一个未经授权的访问异常 用户名和密码正确,ctx.ValidateCredentials返回true,用户名和密码有权更改AD


我如何才能让它工作?

您应该在创建时指定用户名和密码

PrincipalContext.ValidateCredentials
不会将给定的用户名密码持久保存到
PrincipalContext