Search code examples
c#console-applicationadlds

sAMAccountName is missing from Lightweight Directory Services instance


I'm trying to add users programmatically to AD lDS instance. Here's how I add a user:

string ldap = "LDAP://xxxx";

var root = new DirectoryEntry(ldap);
var cn = "CN=" + "Joe" + "Blow";
var u = root.Children.Add(cn, "user");
//u.Properties["sAMAccountName"].Value = "jblow";
u.Properties["employeeID"].Value = "654321";
u.Properties["sn"].Value = "Blow";
u.Properties["givenName"].Value = "Joe";
u.Properties["comment"].Value = "a note for you";
u.Properties["homePhone"].Value = "55555555";
u.CommitChanges();

If I execute this code it will successfully add the user Joe Blow. However, if I try to add username sAMAccountName I get an error:

The specified directory service attribute or value does not exist.System.Exception {System.DirectoryServices.DirectoryServicesCOMException}

Using ADSI Edit I looked at the properties of the object and I DO NOT see sAMAccountName listed there!

enter image description here

How can I add username to AD LDS instance?


Solution

  • This should provide additional information: INFO

    We typically keep the sAMAccountName and userPrincipalName UPN in sync but that can vary depending on your situation/organization.

    You can try this:

    u.Properties["sAMAccountName"].Add("jblow"); u.Properties["userPrincipalName"].Add("jblow"+ "@" + yourDomain );