Active Directory Get User’s groups using LDAP

來源:互聯網
上載者:User

通過LDAP尋找AD User所屬的ADGroupy

  1         /// <summary>

 2         /// 獲得使用者所屬組的SID
 3         /// </summary>
 4         /// <code>Comes From http://netwenchao.cnblogs.com</code>
 5         /// <returns></returns>
 6         public static IEnumerable<string> GetGroupSidsOfUser(string userLoginName, ADOperator operater)
 7         {
 8             using (DirectorySearcher directorySearcher = new DirectorySearcher(
 9                 new DirectoryEntry(string.Format("LDAP://{0}", operater.ManageDomainName), operater.UserLogonName, operater.Password, AuthenticationTypes.Secure),
10                 string.Format("(&(objectcategory=user)(samaccountname={0}))", GetUserName(userLoginName)),
11                 new string[] { ADUserAttributes.SamAccountName }))
12             {
13                 var result = directorySearcher.FindOne();
14                 if (result != null)
15                 {
16                     DirectoryEntry directoryEntry = result.GetDirectoryEntry();
17                     directoryEntry.RefreshCache(new string[] { ADUserAttributes.TokenGroupsGlobalAndUniversal });
18                     for (int index = 0; index < directoryEntry.Properties[ADUserAttributes.TokenGroupsGlobalAndUniversal].Count; index++)
19                     {
20                         yield return ConvertBinarySidToString((byte[])directoryEntry.Properties[ADUserAttributes.TokenGroupsGlobalAndUniversal][index]);
21                     }
22                 }
23             }
24             yield break;
25         }
26 
27         /// <summary>
28         /// 獲得使用者所屬組的AccountName
29         /// </summary>
30         /// <param name="userLoginName"></param>
31         /// <param name="operater"></param>
32         /// <code>Comes From http://netwenchao.cnblogs.com</code>
33         /// <returns></returns>
34         public static IEnumerable<string> GetGroupsOfUser(string userLoginName, ADOperator operater)
35         {
36             using (DirectorySearcher directorySearcher = new DirectorySearcher(
37                 new DirectoryEntry(string.Format("LDAP://{0}", operater.ManageDomainName), operater.UserLogonName, operater.Password, AuthenticationTypes.Secure),
38                 "",
39                 new string[] { ADUserAttributes.SamAccountName }))
40             {
41                 IList<string> groups = new List<string>();
42                 SearchResult sr = null;
43                 var sids = GetGroupSidsOfUser(userLoginName, operater);
44                 if (!sids.Any()) return null;
45                 foreach (var sid in sids)
46                 {
47                     directorySearcher.Filter = string.Format("objectsid={0}", sid);
48                     sr = directorySearcher.FindOne();
49                     if (null != sr && sr.Properties[ADUserAttributes.SamAccountName].Count > 0) groups.Add(sr.Properties[ADUserAttributes.SamAccountName][0].ToString());
50                 }
51                 return groups;
52             }
53         }

Comes From http://netwenchao.cnblogs.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.