如何在C#中从显示名称获取ActiveDirectory中的用户名?我希望能够使用该用户的显示名称在ActiveDirectory中获取该用户的用户ID。显示名称从数据库中获取并存储在此用户会话期间,使用以下代码获取显示名称:usingSystem.DirectoryServices.AccountManagement;privatestringGetDisplayName(){//设置域上下文PrincipalContextctx=newPrincipalContext(ContextType.Domain);//查找当前登录的用户UserPrincipaluser=UserPrincipal.Current;返回user.DisplayName;这一次,我想要一个名为GetUserIdFromDisplayName()的方法,它返回ActiveDirectory登录名。有任何想法吗?我相信通过使用System.DirectoryServices.AccountManagement(S.DS.AM)命名空间的内置功能,您可以比David的回答更容易做到这一点。基本上,您可以定义域上下文并轻松查找AD中的用户和/或组:usingSystem.DirectoryServices.AccountManagement;privatestringGetUserIdFromDisplayName(stringdisplayName){//设置域上下文using(PrincipalContextctx=newPrincipalContext(ContextType.Domain)){//通过显示名称查找用户UserPrincipaluser=UserPrincipal.FindByIdentity(ctx,displayName);//if(user!=null){returnuser.SamAccountName;//或者你可能需要user.UserPrincipalName;}else{返回string.Empty;我认为没有必要转到底层的DirectoryEntry对象——除非UserPrincipal的属性都不是你想要的。PS:如果按显示名称搜索不起作用(我没有方便的AD来测试它)-您也可以始终使用PrincipalSearcher来查找您的用户:usingSystem.DirectoryServices.AccountManagement;privatestringGetUserIdFromDisplayName(stringdisplayName){//使用(PrincipalContextctx=newPrincipalContext(ContextType.Domain)){//定义一个“按示例查询”主体-在这里,我们搜索一个UserPrincipal//并在UserPrincipalqbeUser=newUserPrincipal(ctx)中传递显示名称;qbeUser.DisplayName=displayName;//创建你的委托人搜索器,传入QBE委托人PrincipalSearchersrch=newPrincipalSearcher(qbeUser);//查找匹配-如果存在UserPrincipaluser=srch.FindOne()asUserPrincipal;if(user!=null){返回user.SamAccountName;//或者你可能需要user.UserPrincipalName;}else{返回string.Empty;}}}UserPrincipal有一个GetUnderlyingObject()方法,它将返回DirectoryEntry。从Principal获取DirectoryEntry:privateDirectoryEntryGetDirectoryEntryFromUserPrincipal(Principaluser){return(DirectoryEntry)user.GetUnderlyingObject();}从域名和帐号获取DirectoryEntry:privateDirectoryEntryGetDirectoryEntryFromDomainAndUsername(stringdomainName,stringuserName){//从NT账户名获取sidvarsid=(SecurityIdentifier)newNTAccount(domainName,accountName).Translate(typeof(SecurityIdentifier));//获取LDAP服务帐户的目录条目varserviceEntry=newDirectoryEntry("LDAP://{address}","serviceUsername","servicePassword");varmySearcher=newDirectorySearcher(serviceEntry){Filter=string.Format("(&(ObjectSid={0}))",sid.Value)};返回mySearcher.FindOne().GetDirectoryEntry();}曾经有了DirectoryEntry使用Guid属性来获取条目的Object-GuidprivateGuidGetObjectGuidFromDirectoryEntry(DirectoryEntryentry){//returntheGuidthisistheObject-Guid(ignoreNativeGuid)returnentry.Guid;}要根据目录帐户跟踪应用程序中的用户帐户,请始终将Object-Guid用作“创建对象时无法更改此值”等),则NT和SAM帐户名称可能会更改,不应用于跟踪用户。获取NT帐户名(域用户名):privatestringGetNTAccountNameFromDirectoryEntry(DirectoryEntryentry){PropertyValueCollectionpropertyValueCollection=entry.Properties["objectsid"];SecurityIdentifiersid=newSecurityIdentifier((byte[])propertyValueCollection[0],0);NTAccountntAccount=(NTAccount)sid.Translate(typeof(NTAccount));返回account.ToString();}获取SAM账户名(用户名@域):privatestringGetSAMAccountFromDirectoryEntry(DirectoryEntryentry){returnentry.Properties["Name"].Value;}这是所有ActiveDirectory属性的详尽列表。从Properties获取值时,使用“Ldap-Display-Name”,例如Properties["Ldap-Display-Name"]DisplayName(FirstNameMILastName)可能会派上用场。以上是C#学习教程:如何从C#中的显示名称获取ActiveDirectory中的用户名?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
