How can I search Active Directory by username using C#?(如何使用 C# 按用户名搜索 Active Directory?)
问题描述
我正在尝试通过用户名admin"搜索活动目录.我知道目录中存在具有该用户名的用户,但搜索一直没有结果.
I'm trying to search active directory by the username 'admin'. I know for a fact that there is a user with that username in the directory, but the search keeps coming back with nothing.
var attributeName = "userPrincipalName";
var searchString = "admin"
var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com")
var mySearcher = new DirectorySearcher(ent);
mySearcher.Filter = string.Format("(&(objectClass=user)({0}={1}))", attributeName, searchString);
var userResult = mySearcher.FindOne();
userResult 总是以 null 结束.我很想知道为什么,一定有我遗漏的东西.
userResult always ends up null. I would love to know why, there must be something that I'm missing.
推荐答案
原来userPrincipalName"需要全部小写(userprincipalname").很高兴知道,感谢您的回复.
It turns out that "userPrincipalName" needed to be all lower-case ("userprincipalname"). Good to know, thanks for your responses.
这篇关于如何使用 C# 按用户名搜索 Active Directory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C# 按用户名搜索 Active Directory?
基础教程推荐
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 创建属性设置器委托 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01