Is there a .NET class that can parse CN= strings out of LDAP?(是否有一个 .NET 类可以从 LDAP 中解析 CN= 字符串?)
问题描述
我有一个从 LDAP 获取的用于 Active Directory 组成员资格的字符串,我需要对其进行解析以检查用户是否是 AD 组的成员.有可以为我解析这个的类吗?
例子:
CN=Foo 组名,DC=mydomain,DC=com
另外,如果你在AD中查询一个组成员,你可以直接比较所有成员的distinguishedName,而不需要通过System.DirectoryServices
命名空间的>DirectoryEntry 类.
否则,我只是不知道某处有这样的课程.=)
希望这无论如何都能有所帮助!
编辑#1
这是一个链接,我从中学到了很多使用 AD 和 System.DirectoryServices
命名空间的知识:Howto:(几乎)通过 C# 在 Active Directory 中的所有内容
我会在几天后为您提供一个示例代码,如果您仍然需要它,我将在其中使用 System.DirectoryServices.DirectorySearcher
对象类来检索组的成员.
我希望这个链接能像对我一样帮助你!=)
编辑#2
这是我告诉你的代码示例.这应该可以提高查询 AD 的效率,而无需在 AD 中反复工作.
public IListGetMembers(字符串组名){if (string.IsNullOrEmpty(groupName))抛出新的 ArgumentNullException("groupName");IList<字符串>成员=新列表<字符串>();DirectoryEntry root = new DirectoryEntry(@"LDAP://my.domain.com");DirectorySearcher searcher = new DirectorySearcher();searcher.SearchRoot = 根;searcher.SearchScope = SearchScope.子树;searcher.PropertiesToLoad.Add("member");searcher.Filter = string.Format("(&(objectClass=group)(sAMAccountName={0}))", groupName);SearchResult 结果 = searcher.FindOne();DirectoryEntry groupFound = result.GetDirectoryEntry();for (int index = 0; index < ((object[])groupFound.Properties["member"].Value).Length; ++index)members.Add((string)((object[])groupFound.Properties["member"].Value)[index]);返回会员;}
<块引用>
免责声明:此代码按原样提供.我在我的本地机器上测试了它,它工作得很好.但是因为不能直接复制粘贴,所以不得不在这里重新输入,我可能在输入时犯了一些错误,我希望不要发生这种情况.
I've got a string that I'm fetching from LDAP for Active Directory group membership and I need to parse it to check if the user is a member of the AD group. Is there a class that can parse this for me?
Example:
CN=Foo Group Name,DC=mydomain,DC=com
Besides, if you query the AD for a group members, you'll be able to compare all of the members' distinguishedName's directly without parsing code through the DirectoryEntry
class of the System.DirectoryServices
namespace.
Otherwise, I just don't know of such a class somewhere. =)
Hope this helps anyway somehow !
EDIT #1
Here's a link from which I have learned a lot working with the AD and the System.DirectoryServices
namespace: Howto: (Almost) Everything In Active Directory via C#
I shall provide you with a sample code in a few days, if you still require it, where I will use the System.DirectoryServices.DirectorySearcher
object class to retrieve the members of a group.
I hope this link will help you as it did for me! =)
EDIT #2
Here's the code sample I told you about. This should make it more efficient to query against the AD without having to work bakc and forth the AD.
public IList<string> GetMembers(string groupName) {
if (string.IsNullOrEmpty(groupName))
throw new ArgumentNullException("groupName");
IList<string> members = new List<string>();
DirectoryEntry root = new DirectoryEntry(@"LDAP://my.domain.com");
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = root;
searcher.SearchScope = SearchScope.Subtree;
searcher.PropertiesToLoad.Add("member");
searcher.Filter = string.Format("(&(objectClass=group)(sAMAccountName={0}))", groupName);
SearchResult result = searcher.FindOne();
DirectoryEntry groupFound = result.GetDirectoryEntry();
for (int index = 0; index < ((object[])groupFound.Properties["member"].Value).Length; ++index)
members.Add((string)((object[])groupFound.Properties["member"].Value)[index]);
return members;
}
Disclaimer : This code is provided as-is. I tested it on my local machine and it works perfectly fine. But since I had to retype it here because I couldn't just copy-paste it, I have perhaps made some mistakes while typing, which I wish didn't occur.
这篇关于是否有一个 .NET 类可以从 LDAP 中解析 CN= 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否有一个 .NET 类可以从 LDAP 中解析 CN= 字符串?


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01