Как добавить атрибут Manager в активный каталог с помощью PrincipalContext, используя ASP.net, C#

Я создаю пользователя с помощью PrincipalContext в активном каталоге. Я хочу добавить некоторые дополнительные атрибуты, такие как Location, Initials,EmployeeId и Manager.

Я могу успешно добавить Location, Initials и EmployeeId, используя класс UserPrincipalEx. но я не могу присвоить значение атрибуту Manager.

Когда я пытаюсь присвоить некоторые значения атрибуту Manager, появляется сообщение об ошибке Произошло нарушение ограничения".

Вот мой код:

 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 

Мой класс расширения:

 [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);
    }
}

Пожалуйста, помогите мне решить эту проблему и как назначить значения в поле «Менеджер»?


person Suryakavitha    schedule 08.06.2014    source источник
comment
Где вы берете UserPrincipalEx? Я не могу найти это.   -  person Nathan McKaskle    schedule 11.01.2017
comment
Атрибут менеджера @Suryakavitha недоступен для меня. Я использую .NET версии 4.7.   -  person vbhosale    schedule 31.08.2020


Ответы (1)


Атрибут manager не является произвольным текстом, это ссылка на другого пользователя Active Directory. В этом поле вы должны указать Distinguished Name учетной записи пользователя менеджера.

Например:

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();
person Ashigore    schedule 09.06.2014
comment
при чем здесь контейнер. - person vbhosale; 31.08.2020