twal
twal

Reputation: 7039

Unauthorized Access Exception on UserPrincipal.Save()

i am running the following

  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);
        }
     }

When it runs I get an Unauthorized Access Exception on the User.Save(ctx) The user name and password are correct and ctx.ValidateCredentials returns true and the username and password has rights to make changes to AD

how can i get this to work?

Upvotes: 0

Views: 3049

Answers (1)

Harvey Kwok
Harvey Kwok

Reputation: 11873

You should specify your username and password when you create PrincipalContext.

PrincipalContext.ValidateCredentials doesn't persist your given username password into the PrincipalContext

Upvotes: 2

Related Questions