【发布时间】:2012-10-16 19:34:57
【问题描述】:
我想检查用户是否是 c# 中某个组的成员。该应用程序在 windows mobile 6.1 上运行,我必须使用带有 [DllImport] 的 ldap 函数。
有人有这方面的样品吗? 连接到 ldap 服务器并检查用户/密码是否有效。
【问题讨论】:
标签: c# windows-mobile ldap compact-framework
我想检查用户是否是 c# 中某个组的成员。该应用程序在 windows mobile 6.1 上运行,我必须使用带有 [DllImport] 的 ldap 函数。
有人有这方面的样品吗? 连接到 ldap 服务器并检查用户/密码是否有效。
【问题讨论】:
标签: c# windows-mobile ldap compact-framework
为什么不使用框架中已有的东西。
看看这个:WindowsPrincipal.IsInRole Method (String)
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
principal.IsInRole("role name");
或
如果你使用 C#/VB.Net 和 System.DirectoryServices,这个 sn-p 应该可以解决问题:
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://dc=yourcompany,dc=com");
DirectorySearcher srch = new DirectorySearcher(rootEntry);
srch.SearchScope = SearchScope.Subtree;
srch.Filter = "(&(objectClass=user)(sAMAccountName=yourusername)(memberof=CN=yourgroup,OU=yourOU,DC=yourcompany,DC=com))";
SearchResultCollection res = srch.FindAll();
if(res == null || res.Count <= 0)
Console.WriteLine("This user is NOT a member of this group");
else
Console.WriteLine("This user is INDEED a member of this group");
请注意:这只会测试直接组成员资格,不会测试您域中所谓的“主要组”(通常是“cn=Users”)的成员资格。它不处理嵌套的成员资格,例如用户 A 是 A 组的成员,而 A 组是 B 组的成员 - 用户 A 实际上也是 B 组的成员这一事实并未在此处得到体现。
参考:How to write LDAP query to test if user is member of a group?
【讨论】:
WindowsPrincipal 或 DirectoryEntry。