【问题标题】:Creating user in active directory在活动目录中创建用户
【发布时间】:2010-07-26 06:15:01
【问题描述】:

我要构建一个 webpart 用于在活动目录中创建用户。

为了创建用户帐户,我使用这样的方法:

public string CreateUserAccount(string ldapPath, string userName, 
    string userPassword)
{
    try
    {
        string oGUID = string.Empty;
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
        DirectoryEntry newUser = dirEntry.Children.Add
            ("CN=" + userName, "user");
        newUser.Properties["samAccountName"].Value = userName;
        newUser.CommitChanges();
        oGUID = newUser.Guid.ToString();

        newUser.Invoke("SetPassword", new object[] { userPassword });
        newUser.CommitChanges();
        dirEntry.Close();
        newUser.Close();
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        //DoSomethingwith --> E.Message.ToString();

    }
    return oGUID;
}

执行此方法时出现以下错误:

“服务器无法运行”

【问题讨论】:

    标签: asp.net sharepoint active-directory


    【解决方案1】:

    假设我们安装了域TestDomain.com 的活动目录,并且您有一个名为USERS 的OU(组织单元),并且您在其中有一个名为TestUser 的用户

    所以我们可以说以下

    ldapDomain:完全限定域,如 TestDomain.com 或 dc=contoso,dc=com
    objectPath:对象的完全限定路径:CN= TestUser, OU=USERS, DC=TestDomain, DC=com
    userDn:用户的专有名称:CN=TestUser, OU=USERS, DC=TestDomain, DC=com

    在创建用户时,您应该通过确定其路径(ldap 路径)来确定要创建的位置

    在我们的示例中,我们可以将其视为如下:

    string ldapPath = "LDAP://OU=USERS, DC=TestDomain, DC=com"
    

    有关更多信息,请查看以下链接:
    http://www.selfadsi.org/ldap-path.htm
    http://www.informit.com/articles/article.aspx?p=101405&seqNum=7
    http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.path.aspx

    【讨论】:

      【解决方案2】:

      使用 System.DirectoryServices

      To use this namespace you need to add reference  System.DirectoryServices.dll 
      
             DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local");
      
              for (int i = 3; i < 6; i++)
              {
                  try
                  {
                      DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i, "user");
                      childEntry.CommitChanges();
                      ouEntry.CommitChanges();
                      childEntry.Invoke("SetPassword", new object[] { "password" });
                      childEntry.CommitChanges();
                  }
                  catch (Exception ex)
                  {
      
                  }
              }
      

      使用 System.DirectoryServices.AccountManagement

       To use this namespace you need to add reference  System.DirectoryServices.AccountManagement.dll 
      
                    PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local",           "OU=TestOU,DC=TestDomain,DC=local");
      
              for (int i = 0; i < 3; i++)
              {
                  try
                  {
                      UserPrincipal up = new UserPrincipal(ouContex);
                      up.SamAccountName = "TestUser" + i;
                      up.SetPassword("password");
                      up.Enabled = true;
                      up.ExpirePasswordNow();
                      up.Save();
                  }
                  catch (Exception ex)
                  {
      
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多