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?
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].
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.
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'
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 :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With