如何获取UserPrincipal类未表示的Active Directory属性
我的意思是,现在我正在使用System.DirectoryServices.AccountManagement,如果我使用UserPrincipal类,我只看到名称,中间名等
所以在我的代码中它就像
UserPrincipal myUser = new UserPrincipal(pc); myUser.Name = "aaaaaa"; myUser.SamAccountName = "aaaaaaa"; . . . . myUser.Save();
我怎样才能看到像移动或信息这样的属性?
正确的方法是使用PrincipalExtensions
扩展您所使用的PrincipalExtensions
,并使用ExtensionSet
和ExtensionGet
方法,如此处所述。
在这种情况下,您需要更深入一级 – 返回到DirectoryEntry
– 通过从用户主体中获取它:
DirectoryEntry de = (myUser.GetUnderlyingObject() as DirectoryEntry); if(de != null) { // go for those attributes and do what you need to do }
up.Mobile
是完美的,但不幸的是,UserPrincipal类中没有这样的方法,所以你必须通过调用.GetUnderlyingObject()
来切换到DirectoryEntry。
上述就是C#学习教程:如何获取UserPrincipal类未表示的Active Directory属性分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
static void GetUserMobile(PrincipalContext ctx, string userGuid) { try { UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.Guid, userGuid); DirectoryEntry up_de = (DirectoryEntry)up.GetUnderlyingObject(); DirectorySearcher deSearch = new DirectorySearcher(up_de); deSearch.PropertiesToLoad.Add("mobile"); SearchResultCollection results = deSearch.FindAll(); if (results != null && results.Count > 0) { ResultPropertyCollection rpc = results[0].Properties; foreach (string rp in rpc.PropertyNames) { if (rp == "mobile") Console.WriteLine(rpc["mobile"][0].ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1024743.html