• <legend id='ZdiB6'><style id='ZdiB6'><dir id='ZdiB6'><q id='ZdiB6'></q></dir></style></legend>
  • <small id='ZdiB6'></small><noframes id='ZdiB6'>

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

      1. <tfoot id='ZdiB6'></tfoot>
      2. 在 .NET 中的 Active Directory 组中添加和删除用户

        时间:2023-10-25

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

        • <tfoot id='NeEVf'></tfoot>
          • <legend id='NeEVf'><style id='NeEVf'><dir id='NeEVf'><q id='NeEVf'></q></dir></style></legend>

              <bdo id='NeEVf'></bdo><ul id='NeEVf'></ul>

                  <tbody id='NeEVf'></tbody>

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

                1. 本文介绍了在 .NET 中的 Active Directory 组中添加和删除用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写以下方法来在 C# 中从活动目录中添加和删除用户.

                  I am writing the following methods to add and remove users from active directory in C#.

                  void AddUserToGroup(string userId, string groupName);
                  void RemoveUserFromGroup(string userId, string groupName);
                  

                  如何最好地实现这些方法?

                  How best to implement these methods?

                  这是来自 CodeProject 的一些代码.我看不到在这些示例中指定 AD 服务器的位置吗?(在使用 LDAP 协议时,它是由 .NET 框架隐式提供的吗?).这些例子值得学习吗?

                  Here is some code from CodeProject. I can't see where the AD server is specified in these examples though? (is it implicitly supplied by the .NET framework when using the LDAP protocol?). Are these examples worth following?

                  public void AddToGroup(string userDn, string groupDn)
                  {
                      try
                      {
                          DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
                          dirEntry.Properties["member"].Add(userDn);
                          dirEntry.CommitChanges();
                          dirEntry.Close();
                      }
                      catch (System.DirectoryServices.DirectoryServicesCOMException E)
                      {
                          //doSomething with E.Message.ToString();
                  
                      }
                  }
                  
                  
                  public void RemoveUserFromGroup(string userDn, string groupDn)
                  {
                      try
                      {
                          DirectoryEntry dirEntry = new DirectoryEntry("LDAP://" + groupDn);
                          dirEntry.Properties["member"].Remove(userDn);
                          dirEntry.CommitChanges();
                          dirEntry.Close();
                      }
                      catch (System.DirectoryServices.DirectoryServicesCOMException E)
                      {
                          //doSomething with E.Message.ToString();
                  
                      }
                  }
                  

                  推荐答案

                  呃.LDAP.如果您使用的是 .Net Framework 3.5 或更高版本,我强烈建议您使用 System.DirectoryServices.AccountManagement 命名空间.这让事情如此变得容易多了.

                  Ugh. LDAP. If you're using the .Net Framework 3.5 or above, I highly recommend using the System.DirectoryServices.AccountManagement namespace. That makes things so much easier.

                  public void AddUserToGroup(string userId, string groupName) 
                  { 
                      try 
                      { 
                          using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "COMPANY"))
                          {
                              GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
                              group.Members.Add(pc, IdentityType.UserPrincipalName, userId);
                              group.Save();
                          }
                      } 
                      catch (System.DirectoryServices.DirectoryServicesCOMException E) 
                      { 
                          //doSomething with E.Message.ToString(); 
                  
                      } 
                  } 
                  
                  public void RemoveUserFromGroup(string userId, string groupName)
                  {   
                      try 
                      { 
                          using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "COMPANY"))
                          {
                              GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
                              group.Members.Remove(pc, IdentityType.UserPrincipalName, userId);
                              group.Save();
                          }
                      } 
                      catch (System.DirectoryServices.DirectoryServicesCOMException E) 
                      { 
                          //doSomething with E.Message.ToString(); 
                  
                      }
                  }
                  

                  这篇关于在 .NET 中的 Active Directory 组中添加和删除用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 linq 中设置相等 下一篇:没有了

                  相关文章

                  <legend id='c90Q2'><style id='c90Q2'><dir id='c90Q2'><q id='c90Q2'></q></dir></style></legend>

                    1. <small id='c90Q2'></small><noframes id='c90Q2'>

                    2. <tfoot id='c90Q2'></tfoot>

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