<bdo id='aZ3vh'></bdo><ul id='aZ3vh'></ul>
  • <tfoot id='aZ3vh'></tfoot>

      <small id='aZ3vh'></small><noframes id='aZ3vh'>

      1. <i id='aZ3vh'><tr id='aZ3vh'><dt id='aZ3vh'><q id='aZ3vh'><span id='aZ3vh'><b id='aZ3vh'><form id='aZ3vh'><ins id='aZ3vh'></ins><ul id='aZ3vh'></ul><sub id='aZ3vh'></sub></form><legend id='aZ3vh'></legend><bdo id='aZ3vh'><pre id='aZ3vh'><center id='aZ3vh'></center></pre></bdo></b><th id='aZ3vh'></th></span></q></dt></tr></i><div id='aZ3vh'><tfoot id='aZ3vh'></tfoot><dl id='aZ3vh'><fieldset id='aZ3vh'></fieldset></dl></div>
      2. <legend id='aZ3vh'><style id='aZ3vh'><dir id='aZ3vh'><q id='aZ3vh'></q></dir></style></legend>

        在 C# 中,如何访问 Active Directory 以获取某个用户所属的组列表?

        时间:2023-10-24

        <small id='SSh6H'></small><noframes id='SSh6H'>

      3. <legend id='SSh6H'><style id='SSh6H'><dir id='SSh6H'><q id='SSh6H'></q></dir></style></legend>
        1. <tfoot id='SSh6H'></tfoot>

            <i id='SSh6H'><tr id='SSh6H'><dt id='SSh6H'><q id='SSh6H'><span id='SSh6H'><b id='SSh6H'><form id='SSh6H'><ins id='SSh6H'></ins><ul id='SSh6H'></ul><sub id='SSh6H'></sub></form><legend id='SSh6H'></legend><bdo id='SSh6H'><pre id='SSh6H'><center id='SSh6H'></center></pre></bdo></b><th id='SSh6H'></th></span></q></dt></tr></i><div id='SSh6H'><tfoot id='SSh6H'></tfoot><dl id='SSh6H'><fieldset id='SSh6H'></fieldset></dl></div>

                • <bdo id='SSh6H'></bdo><ul id='SSh6H'></ul>
                    <tbody id='SSh6H'></tbody>
                • 本文介绍了在 C# 中,如何访问 Active Directory 以获取某个用户所属的组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C# 中,我如何访问 Active Directory 以获取某个用户所属的组列表?

                  In C#, how do i access Active Directory to get the list of groups that a certain user belongs to?

                  用户详细信息格式为:

                  "MYDOMAINmyuser"
                  

                  我一直在遵循 此处的说明,但它们仅适用如果我在表单中有用户详细信息:

                  I've been following the instructions from here but they only work if i have the user details in the form:

                  "LDAP://sample.com/CN=MySurname MyFirstname,OU=General,OU=Accounts,DC=sample,DC=com"
                  

                  所以也许我要问的是,如何从第一个较短的表格到下面的完全限定表格?

                  So maybe what i'm asking is, how to get from the first, shorter, form to the fully qualified form below?

                  非常感谢!

                  推荐答案

                  这可能会有所帮助...

                  This might help...

                  using System.Collections;
                  using System.DirectoryServices;
                  
                  /// <summary>
                  /// Gets the list of AD groups that a user belongs to
                  /// </summary>
                  /// <param name="loginName">The login name of the user (domainlogin or login)</param>
                  /// <returns>A comma delimited list of the user's AD groups</returns>
                  public static SortedList GetADGroups(string loginName)
                  {
                      if (string.IsNullOrEmpty(loginName))
                          throw new ArgumentException("The loginName should not be empty");
                  
                      SortedList ADGroups = new SortedList();
                  
                      int backSlash = loginName.IndexOf("\");
                      string userName = backSlash > 0 ? loginName.Substring(backSlash + 1) : loginName;
                  
                      DirectoryEntry directoryEntry = new DirectoryEntry();
                      DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry, "(sAMAccountName=" + userName + ")");
                  
                      SearchResult searchResult = directorySearcher.FindOne();
                      if (null != searchResult)
                      {
                          DirectoryEntry userADEntry = new DirectoryEntry(searchResult.Path);
                  
                          // Invoke Groups method.
                          object userADGroups = userADEntry.Invoke("Groups");
                          foreach (object obj in (IEnumerable)userADGroups)
                          {
                              // Create object for each group.
                              DirectoryEntry groupDirectoryEntry = new DirectoryEntry(obj);
                              string groupName = groupDirectoryEntry.Name.Replace("cn=", string.Empty);
                              groupName = groupName.Replace("CN=", string.Empty);
                              if (!ADGroups.ContainsKey(groupName))
                                  ADGroups.Add(groupName, groupName);
                          }
                      }
                  
                      return ADGroups;
                  }
                  

                  这篇关于在 C# 中,如何访问 Active Directory 以获取某个用户所属的组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:ActiveDirectoryMembershipProvider 验证用户 下一篇:尝试使用 LDAP 连接连接 AD 时出错

                  相关文章

                    <bdo id='APXXc'></bdo><ul id='APXXc'></ul>
                  <legend id='APXXc'><style id='APXXc'><dir id='APXXc'><q id='APXXc'></q></dir></style></legend>
                • <small id='APXXc'></small><noframes id='APXXc'>

                • <tfoot id='APXXc'></tfoot>

                • <i id='APXXc'><tr id='APXXc'><dt id='APXXc'><q id='APXXc'><span id='APXXc'><b id='APXXc'><form id='APXXc'><ins id='APXXc'></ins><ul id='APXXc'></ul><sub id='APXXc'></sub></form><legend id='APXXc'></legend><bdo id='APXXc'><pre id='APXXc'><center id='APXXc'></center></pre></bdo></b><th id='APXXc'></th></span></q></dt></tr></i><div id='APXXc'><tfoot id='APXXc'></tfoot><dl id='APXXc'><fieldset id='APXXc'></fieldset></dl></div>