[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);}
然后使用扩展类FindByIDentity方法进行搜索
var user = User.FindByIDentity( DomainContext,"name");
见这Link
总结以上是内存溢出为你收集整理的c# – 扩展UserPrincipal类全部内容,希望文章能够帮你解决c# – 扩展UserPrincipal类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)