我做了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,identityType,identityValue); }
var user = User.FindByIdentity( DomainContext,"name" );
见这Link
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。