从 AD 轻量级目录服务访问 userProxy 和 userProxyFull
从 AD 轻量级目录服务访问 userProxy 和 userProxyFull
引言
通过 C# 管理类(System.DirectoryServices.Accountmanagement
)访问 AD LDS 中的 userProxy 和 userProxyFull 对象。C# 中的 LDAP API 只有几个类来访问用户和组。本文档解释了如何扩展主类以访问 userProxy
、userProxyFull
、person 等类。背景
最近,我参与了一个项目,该项目涉及扩展 AD LDS 实例中的用户模式。这需要能够存储针对用户的特定于应用程序的数据。因此,我们扩展了 userProxy
和 userProxyFull
类的模式以添加一些额外的自定义属性。
可以通过 UserPrincipal
和 GroupPrincipal
类以一种直接的方式访问用户和组类,但如果您想访问 userProxy
、userProxyFull
或 Person
类,这些类将没有用处。正如我在网上搜索到的那样,没有太多文档或在线帮助来通过管理类访问这些对象,因此我尝试在这里解释如何扩展 UserPrincipal
类以访问所述对象。
在这里,我扩展了 UserPrincipal
类以从 AD LDS 或 AD DS 访问此类对象。
这是代码
这里,我们创建了一个类来访问 userProxy
,您可以创建类似的扩展类来访问其他对象,例如 userProxyFull
和 person 等...
//
using System;
using System.DirectoryServices.AccountManagement;
/// <summary>
/// Principal Extensions Class
///
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("userProxy")]
public class UserProxyPrincipal : UserPrincipal
{
// Inplement the constructor using the base class constructor.
public UserProxyPrincipal(PrincipalContext context)
: base(context)
{
}
// Implement the constructor with initialization parameters.
public UserProxyPrincipal(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
: base(context,
samAccountName,
password,
enabled)
{
}
UserProxyPrincipalSearchFilter searchFilter;
new public UserProxyPrincipalSearchFilter AdvancedSearchFilter
{
get
{
if (null == searchFilter)
searchFilter = new UserProxySearchFilter(this);
return searchFilter;
}
}
//CUSTOM ATTRIBUTE
[DirectoryProperty("User-PostingID")]
public string PostingId
{
get
{
if (ExtensionGet("User-PostingID").Length != 1)
return null;
return (string)ExtensionGet("User-PostingID")[0];
}
set
{
ExtensionSet("User-PostingID", value);
}
}
[DirectoryProperty("distinguishedName")]
public string DistinguishedName
{
get
{
if (ExtensionGet("distinguishedName").Length != 1)
return null;
return (string)ExtensionGet("distinguishedName")[0];
}
set
{
ExtensionSet("distinguishedName", value);
}
}
// Create the other home phone property.
[DirectoryProperty("otherHomePhone")]
public string[] HomePhoneOther
{
get
{
int len = ExtensionGet("otherHomePhone").Length;
string[] otherHomePhone = new string[len];
object[] otherHomePhoneRaw = ExtensionGet("otherHomePhone");
for (int i = 0; i < len; i++)
{
otherHomePhone[i] = (string)otherHomePhoneRaw[i];
}
return otherHomePhone;
}
set
{
ExtensionSet("otherHomePhone", value);
}
}
// Create the logoncount property.
[DirectoryProperty("LogonCount")]
public Nullable<int> LogonCount
{
get
{
if (ExtensionGet("LogonCount").Length != 1)
return null;
return ((Nullable<int>)ExtensionGet("LogonCount")[0]);
}
}
// Implement the overloaded search method FindByIdentity.
public static new UserProxyPrincipal FindByIdentity(PrincipalContext context,
string identityValue)
{
return (UserProxyPrincipal)FindByIdentityWithType(context,
typeof(UserProxyPrincipal),
identityValue);
}
// Implement the overloaded search method FindByIdentity.
public static new UserProxyPrincipal FindByIdentity(PrincipalContext context,
IdentityType identityType,
string identityValue)
{
return (UserProxyPrincipal)FindByIdentityWithType(context,
typeof(UserProxyPrincipal),
identityType,
identityValue);
}
}
public class UserProxyPrincipalSearchFilter : AdvancedFilters
{
public UserProxyPrincipalSearchFilter(Principal p) : base(p) { }
public void LogonCount(int value, MatchType mt)
{
this.AdvancedFilterSet("LogonCount", value, typeof(int), mt);
}
}
//
UserPrincipal
类被扩展以保存 userProxy
对象,如代码所示,该类用标签 [DrectoryObjectClass("userProxy")
] 装饰,这使得能够从 LDS 使用 userProxy
对象。我们可以为 userProxyFull
和 person 等创建类似的类来访问数据对象。
使用代码
/// <summary>
/// Getting all userProxies of AD LDS and adding them into List.
///
public List GetAllUserProxies()
{
try
{
PrincipalContext oPrincipal = GetPrincipalContext();
UserProxyPrincipal inetPerson = UserProxyPrincipal.FindByIdentity(oPrincipal,
IdentityType.Name,
"pkola");
UserProxyPrincipal up = new UserProxyPrincipal(oPrincipal);
PrincipalSearcher srch = new PrincipalSearcher(up);
List list = new List();
foreach (var item in srch.FindAll())
{
list.Add(item as UserProxyPrincipal);
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
希望这能帮助您在用特定于应用程序的数据扩展 LDS 用户模式时。