Suryakavitha
Suryakavitha

Reputation: 1411

How to add Manager attribute on active directory by PrincipalContext using ASP.net,C#

I am creating user by PrincipalContext on active directory. I want to add some extra attributes like Location, Initials,EmployeeId and Manager.

I can able to add Location, Initials and EmployeeId successfully by using UserPrincipalEx class. but i am not able assign value for Manager attribute.

When I am trying to assign some values on Manager attribute, it will show an error message as A constraint violation occurred."

Here My Code is:

 PrincipalContext ouContext = new PrincipalContext(ContextType.Domain, AD.ServerDomain, container, AD.LDAPUser, AD.LDAPPasswoprd);
 UserPrincipalEx user = new UserPrincipalEx(ouContext);
 user.GivenName = txtGivenName.Text;
 user.Surname = txtSurName.Text;
 user.DisplayName = Convert.ToString(txtDisplayName.Text);
 user.Manager = txtSupervisor.Text; // What should I assign in this field
 user.SetPassword("welcome1*");
 user.Enabled = true;
 user.ExpirePasswordNow();
 user.Save();    // Here I am getting the error 

My extension class:

 [DirectoryObjectClass("user")]

 [DirectoryRdnPrefix("CN")]

  public class UserPrincipalEx : UserPrincipal

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


public static new UserPrincipalEx FindByIdentity(PrincipalContext context,
                                               string identityValue)
{
    return (UserPrincipalEx)FindByIdentityWithType(context,
                                                 typeof(UserPrincipalEx),
                                                 identityValue);
}
public static new UserPrincipalEx FindByIdentity(PrincipalContext context,
                                               IdentityType identityType,
                                               string identityValue)
{
    return (UserPrincipalEx)FindByIdentityWithType(context,
                                                 typeof(UserPrincipalEx),
                                                 identityType,
                                                 identityValue);
}
   [DirectoryProperty("distinguishedName")]
public string DistinguishedName
{

    get
    {
        if (ExtensionGet("distinguishedName").Length != 1)
            return null;


        return (string)ExtensionGet("distinguishedName")[0];


    }
    set
    {
        ExtensionSet("distinguishedName", value);
    }
}

[DirectoryProperty("manager")]
public string Manager
{

    get
    {
        if (ExtensionGet("manager").Length != 1)
            return null;


        return (string)ExtensionGet("manager")[0];


    }
    set
    {
        ExtensionSet("manager", value);
    }
}

Please help me to resolve this problem and How to assign values on Manager field?

Upvotes: 0

Views: 5889

Answers (1)

Ashigore
Ashigore

Reputation: 4678

The manager attribute is not free text, it is a link to another Active Directory user. In this field you should specify the Distinguished Name of the manager's user account.

For Example:

PrincipalContext ouContext = new PrincipalContext(ContextType.Domain, AD.ServerDomain, container, AD.LDAPUser, AD.LDAPPasswoprd);
UserPrincipalEx user = new UserPrincipalEx(ouContext);
user.Manager = "CN=Managing Person,OU=Users,OU=Organisation,DC=domain,DC=local";
user.Save();

Upvotes: 2

Related Questions