c# - 如何在 C# 中的 Active Directory 中的 UserPrincipal 对象上设置管理器属性

标签 c# active-directory

我正在尝试在此处记录的 UserPrincipal 类型的对象上设置属性 Manager:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms680857(v=vs.85).aspx

但不能简单的说

UserPrincipal.Manager = "some value" 

有人可以向我解释一下这是如何工作的吗?谢谢!

最佳答案

S.DS.AM 命名空间中的基本 UserPrincipal 不具有该属性 - 但您可以扩展用户主体类并添加您需要的其他属性。

在这里阅读更多相关信息:

Managing Directory Security Principals in the .NET Framework 3.5
(在文章末尾有一个关于可扩展性的部分)

代码如下:

[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
    // Inplement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEx(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled) : base(context, samAccountName, password, enabled)
    {} 

    // Create the "Manager" property.    
    [DirectoryProperty("manager")]
    public string Manager
    {
        get
        {
            if (ExtensionGet("manager").Length != 1)
                return string.Empty;

            return (string)ExtensionGet("manager")[0];
        }
        set { ExtensionSet("manager", value); }
    }

    // Implement the overloaded search method FindByIdentity.
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
    }
}

现在您可以找到并使用 UserPrincipalEx 类,它具有供您使用的 .Manager 属性:

UserPrincipalEx userEx = UserPrincipalEx.FindByIdentity(ctx, "YourUserName");

// the .Manager property contains the DN (distinguished name) for the manager of this user
var yourManager = userEx.Manager;

关于c# - 如何在 C# 中的 Active Directory 中的 UserPrincipal 对象上设置管理器属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298811/

相关文章:

javascript - observablearray 中的对象 KnockoutJs

c# - 事件处理中的静态变量

active-directory - Active Directory 林信任是否具有传递性?

c# - 委托(delegate)逆变(参数兼容性)

c# - 关于 XAML、代码隐藏和 MVVM

python - 是否可以从 cPython 运行 PowerShell 和 Active Directory 命令?

java - Java中如何通过LDAP获取AD组的所有成员

Azure AD B2C 自定义策略 : Add hyperlink to custom attribute ClaimType

powershell - (AD) PowerShell 中的对象相等性

c# - 从 C# 应用程序编辑 Word .docx 文件