如何使用ASP.net、c#在PrincipalContext中为活动目录添加Manager属性

本文关键字:活动 添加 属性 Manager PrincipalContext ASP 何使用 net | 更新日期: 2023-09-27 18:07:20

我正在活动目录上通过PrincipalContext创建用户。我想添加一些额外的属性,如Location, Initials,EmployeeId和Manager。

我可以通过使用UserPrincipalEx类成功添加Location, Initials和EmployeeId。但是我不能为Manager属性赋值。

当我试图在Manager属性上分配一些值时,它会显示一个错误消息约束违反发生。"

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 

扩展类:

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

请帮助我解决这个问题,以及如何在管理器字段上赋值?

如何使用ASP.net、c#在PrincipalContext中为活动目录添加Manager属性

manager属性不是自由文本,它是指向另一个Active Directory用户的链接。在此字段中,您应该指定管理员的用户帐户的专有名称。

例如:

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();