我正在尝试在此处记录的 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/