从本地网络上的活动目录中读取用户登录名的问题
本文关键字:用户 读取 登录 问题 本地网络 活动 | 更新日期: 2023-09-27 17:48:54
Az 你知道在 active directory 中定义的每个用户都有 FirstName,LastName,UserLoginName(sAMAccountName),Email,....现在我有本地网络上任何用户的名字+姓氏,我想从服务器的活动目录中获取任何用户的用户名。我该怎么办?这是我的代码::
DirectoryServices.SearchResult myResult;
string filterString = string.Empty;
string EntryString = "LDAP:// MyDomain";
DirectoryServices.DirectorySearcher myDirectorySearcher = new DirectoryServices.DirectorySearcher(new DirectoryServices.DirectoryEntry(EntryString));
string tempStr;
string[] splStr = new string[3];
filterString = "CN="+FisrtAndLastNameOfUser;
myDirectorySearcher.Filter = filterString;
myDirectorySearcher.PropertiesToLoad.Add("UserName");
myResult = myDirectorySearcher.FindOne();
splStr = Regex.Split(myResult.Properties("UserName").Item(0).ToString, " ");
tempStr = splStr(1).ToString + " " + splStr(0).ToString;
Label1.Text = "Hello " + tempStr;
输出 splStr=null;我的错在哪里??多谢。
如果你使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有关于它的信息:
在 .NET Framework 3.5 中管理目录安全主体
基本上,您可以定义域上下文并在 AD 中轻松查找用户和/或组:
// set up domain context for default domain
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find user by name
UserPrincipal user = UserPrincipal.FindByIdentity(FirstAndLastNameOfUser);
// if found - access any of its properties
if(user != null)
{
string userLoginName = user.SamAccountName; // or whatever else you're looking for
}
新的 S.DS.AM 使得在AD中玩用户和组变得非常容易: