检查用户是否是组的成员(ldap)

时间:2012-10-16 05:35:52

标签: c# windows-mobile ldap compact-framework

我想检查用户是否是c#中组的成员。该应用程序在Windows Mobile 6.1上运行,我必须使用[DllImport]的ldap函数。

有人为此提供样品吗? 连接到ldap服务器并检查用户/密码是否有效。

1 个答案:

答案 0 :(得分:1)

为什么不使用框架中已有的内容。

看看这个:WindowsPrincipal.IsInRole Method (String)

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
principal.IsInRole("role name");

OR

如果你使用C#/ VB.Net和System.DirectoryServices,这个代码片段可以解决这个问题:

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 =用户”)的成员身份。它不处理嵌套的成员资格,例如用户A是A组的成员,该组是B组的成员 - 事实上用户A实际上是B组的成员,但这里没有反映出来。

参考:How to write LDAP query to test if user is member of a group?