我如何设置UserPrincipal对象的管理属性在Active Directory中的C# [英] How do I set the Manager Attribute on the UserPrincipal object in Active Directory in C#

查看:153
本文介绍了我如何设置UserPrincipal对象的管理属性在Active Directory中的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想键入 UserPrincipal 这里记录的对象上设置属性管理​​

I am trying to set the attribute Manager on an object of type UserPrincipal documented here:

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

但不能简单地说

UserPrincipal.Manager = "some value" 

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

Can someone please explain to me how this works? Thanks!

推荐答案

基本 UserPrincipal 在S.DS.AM命名空间不具有的属性 - 但你可以扩展用户的主要类,并添加所需的附加属性。

The basic UserPrincipal in the S.DS.AM namespace does not feature that attribute - but you can extend the user principal class and add additional attributes that you need.

了解更多关于它的:

在.NET Framework管理目录安全主体3.5
有一节的扩展对文章的末尾的)

Managing Directory Security Principals in the .NET Framework 3.5
(there's a section on extensibility towards the end of the article)

这里的code:

[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 属性供您使用的工作:

Now you can find and work with a UserPrincipalEx class which has the .Manager property for you to use:

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

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

这篇关于我如何设置UserPrincipal对象的管理属性在Active Directory中的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆