Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding address information to active directory users

I'm using System.DirectoryServices.AccountManagement namespace classes to add and manage users in AD, but I can't seem to find how to add Address information to user objects. I'm using the UserPrincipal class to add users programatically to AD.

Any ideas?

like image 369
Wessam Zeidan Avatar asked May 02 '11 09:05

Wessam Zeidan


People also ask

How do I add additional email addresses for a user in Active Directory?

Open the properties page for the user (Right-click user and select Properties). Select the Email Addresses tab. Click the New button and select SMTP Address, then click OK. Enter the email address, for example [email protected].

How do I add data to Active Directory?

In the Service Manager console, click Administration. In the Administration pane, expand Administration, and then click Connectors. In the Connectors pane, select the Active Directory connector that you want to synchronize. In the Tasks pane, under the name of the connector, click Synchronize Now.

How do I add computer description in Active Directory?

On the 'security' tab, click the 'advanced' button. Click the 'add' button, type 'Authenticated Users'. Then click OK. In the permission entry dialogue, set the 'apply to' pull-down menu to 'Descendant Computer Objects', then in the permissions section, tick the allow options for 'Write Description'


1 Answers

Here is a sample to do that by using extensibility call :

  class DSPrincipals
  {
    static void Main(string[] args)
    {
      /* Retreiving a principal context
       */
      PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "ou=Monou,dc=dom,dc=fr", "jpb", "pass@1w0rd01");


      /* Create a user principal object
       */
      slxUser aSlxUser = new slxUser(domainContextMonou, "W.Zeidan", "pass@1w0rd01", true);

      /* assign some properties to the user principal
       */
      aSlxUser.GivenName = "Wessam";
      aSlxUser.Surname = "Zeidan";
      aSlxUser.streetAddress = "Add1";


      /* Force the user to change password at next logon
       */
      aSlxUser.ExpirePasswordNow();

      /* save the user to the directory
       */
      aSlxUser.Save();


      Console.ReadLine();
    }
  }

  [DirectoryObjectClass("user")]
  [DirectoryRdnPrefix("CN")]
  class slxUser : UserPrincipal
  {
    public slxUser(PrincipalContext context)
      : base(context) { }

    public slxUser(PrincipalContext context, string samAccountName, string password,  bool enabled ) : base(context, samAccountName, password, enabled)
    {
    }

    [DirectoryProperty("streetAddress")]
    public string streetAddress
    {
      get
      {
        object[] result = this.ExtensionGet("streetAddress");
        if (result != null)
        {
          return (string)result[0];
        }
        else
        {
          return null;
        }
      }
      set { this.ExtensionSet("streetAddress", value); }
    }
  }

You'll find more information in MSDN documentation.

Here is the result :

enter image description here

like image 145
JPBlanc Avatar answered Oct 24 '22 21:10

JPBlanc