0

I don't have that much experience with AD but from looking online I've found how to programatically create users through C#. I've also read up on custom attributes and what I was hoping to do was to create a new custom attribute, let's say 'myNewAttribute' and populate it as part of the user creation.

I had thought that if I add this custom attribute in AD then it should be possible to populate it on every account creation but maybe it needs to be created each time?

Assuming it does not need to be created each time, up until now I've created users by doing something like:

using(var pc = new PrincipalContext(ContextType.Domain, <SERVER HERE>,username,password))
{
    using(var up = new UserPrincipal(pc)){
        up.UserPrincipalName = "whatever";
        up.Save();
    }
}

What I would ideally like to be able to do is:

up.myNewAttribute = "new value here";

Is this possible or if not how can I set the attribute value here?

3
  • Does your custom attribute already exists in AD? And you just want to set/update the value using C#. Commented Apr 26, 2019 at 10:01
  • It will, I am planning on creating it in AD (adding to the schema I believe is what happens) and setting the value of it through code. Yes, updating / setting the value. Commented Apr 26, 2019 at 10:02
  • 1
    You can use DirectoryEntry for this. Please check stackoverflow.com/questions/36100121/… Commented Apr 26, 2019 at 10:15

1 Answer 1

1

The UserPrincipal class only exposes certain attributes. For any others, you have to access the underlying DirectoryEntry object and modify it directly:

((DirectoryEntry) up.GetUnderlyingObject()).Properties["myNewAttribute"].Value = "new value here";

If it's something you'll be doing frequently, another option is to create your own class that inherits from UserPrincipal and exposes that attribute as a property. There is an example with GroupPrincipal here, but it applies just the same to UserPrincipal: https://anyrest.wordpress.com/2010/10/14/how-to-use-ad-attributes-not-represented-in-userprincipal-groupprincipal-and-computerprincipal/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.