我做UserPrincipal类检索我需要一些缺少的属性的扩展:
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
class UserPrincipalExt : UserPrincipal
{
public UserPrincipalExt(PrincipalContext context)
: base(context)
{
}
[DirectoryProperty("department")]
public string Department
{
get
{
if (ExtensionGet("department").Length != 1)
return null;
return (string)ExtensionGet("department")[0];
}
set
{
this.ExtensionSet("department", value);
}
}
[DirectoryProperty("company")]
public string Company
{
get
{
if (ExtensionGet("company").Length != 1)
return null;
return (string)ExtensionGet("company")[0];
}
set
{
this.ExtensionSet("company", value);
}
}
[DirectoryProperty("c")]
public string CountryAbbreviation
{
get
{
if (ExtensionGet("c").Length != 1)
return null;
return (string)ExtensionGet("c")[0];
}
set
{
this.ExtensionSet("c", value);
}
}
}
然后,我可以这样轻松地搜索:
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, myDomain);
UserPrincipalExt userExt = new UserPrincipalExt(principalContext);
PrincipalSearcher searcher = new PrincipalSearcher(userExt);
userExt.GivenName = "blabla";
userExt.EmailAddress ="text here";
PrincipalSearchResult<Principal> searchTmp = null;
searcher.QueryFilter = userExt;
searchTmp = searcher.FindAll();
所以,我的新任务,和我目前的问题,是这样的:在ActiveDirectory中搜索组,就必须获得用户的列表,使用扩展的类,当然。
GroupPrincipal group = (GroupPrincipal)collection.FirstOrDefault();
foreach (Principal pRes in group.GetMembers())
{
//This doesnt work of course.
// return null value.
UserPrincipalExt user = pRes as UserPrincipalExt;
}
我怎样才能达到目标?
至于解决方法我已经做了功能反正检索的属性:
private string GetExtendedProperty(Principal principal, string propertyTo)
{
string property = "";
try
{
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties.Contains(propertyTo))
{
property = directoryEntry.Properties[propertyTo].Value.ToString();
}
else
{
property = "";
}
}
catch (Exception ex)
{
Logger.ScriviLog(4, this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex.Message);
}
return property;
}
先感谢您。
覆盖在你的扩展类的FindByIdentity方法。
public new static User FindByIdentity(PrincipalContext context, string identityValue)
{
return (User)FindByIdentityWithType(context, typeof(User), identityValue);
}
public new static User FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (User)FindByIdentityWithType(context, typeof(User), identityType, identityValue);
}
然后搜索使用扩展的类FindByIdentity方法
var user = User.FindByIdentity(
DomainContext,
"name"
);
看到这个Link
dblock247有正确答案
public new static UserPrincipalExt FindByIdentity(PrincipalContext context, string identityValue)
{
return (UserPrincipalExt)FindByIdentityWithType(context, typeof(UserPrincipalExt), identityValue);
}
public new static UserPrincipalExt FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
{
return (UserPrincipalExt)FindByIdentityWithType(context, typeof(UserPrincipalExt), identityType, identityValue);
}
你可以得到本金这样再
UserPrincipalExt oUserPrincipal = UserPrincipalExt.FindByIdentity(oPrincipalContext, IdentityType.UserPrincipalName, userName);
和属性是这样的:
Console.WriteLine(oUserPrincipal.Company);