0

C#スクリプトを使用してActiveDirectoryにユーザーを追加しようとしています。私はインターネットでこのスクリプトを見つけました(私はそれを自分で作成しませんでした)。問題は、ユーザーを追加しようとすると次のエラーが発生することです。

指定されたディレクトリサービスの属性または値が存在しません。

これは私が今持っているコードです:

private void buttonCreateUser_Click(object sender, EventArgs e)
{
    CreateADSUser(textboxUsername.Text, textboxPassword.Text);
}

public string CreateADSUser(string username, string password)
{
    String RootDSE;
    try
    {
        DirectorySearcher DSESearcher = new DirectorySearcher();
        RootDSE = DSESearcher.SearchRoot.Path;

        RootDSE = RootDSE.Insert(7, "CN=Users,");

        DirectoryEntry myDE = new DirectoryEntry(RootDSE);
        DirectoryEntries myEntries = myDE.Children;

        DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" + username, "user");
        myDirectoryEntry.Properties["userPrincipalName"].Value = username;
        myDirectoryEntry.Properties["name"].Value = username;
        myDirectoryEntry.Properties["Password"].Value = password;
        myDirectoryEntry.Properties["samAccountName"].Value = username;
        myDirectoryEntry.Properties["FullName"].Value = username;
        myDirectoryEntry.Properties["AccountDisabled"].Value = 0;
        myDirectoryEntry.Properties["PasswordRequired"].Value = 1;

        // Permanent Password?
        myDirectoryEntry.Properties["permpass"].Value = 1;
        myDirectoryEntry.CommitChanges();

        DSESearcher.Dispose();
        myDirectoryEntry.Dispose();

        textboxReports.Text = "Worked!";
        return "Worked!";
    }
    catch (Exception ex)
    {
        textboxReports.Text = ex.Message;
        return ex.Message;
    }
}
4

3 に答える 3

1

気にしないでください、私は修正を持っています!

これは現在のように見えます:

using (var pc = new PrincipalContext(ContextType.Domain))
            {
                using (var up = new UserPrincipal(pc))
                {
                    up.SamAccountName = textboxUsername.Text; // Username
                    up.EmailAddress = textboxEmail.Text; // Email
                    up.SetPassword(textboxPassword.Text); // Password
                    up.Enabled = true;
                    up.ExpirePasswordNow();
                    up.Save();
                }
            }
于 2012-05-30T22:57:19.647 に答える
1

ここでの問題は、これらのプロパティが実際には存在しないことです。

myDirectoryEntry.Properties["Password"].Value = password; 
myDirectoryEntry.Properties["FullName"].Value = username; 
myDirectoryEntry.Properties["AccountDisabled"].Value = 0; 
myDirectoryEntry.Properties["PasswordRequired"].Value = 1; 
myDirectoryEntry.Properties["permpass"].Value = 1; 

これはあなたが書いたものではありません:

myDirectoryEntry.Properties["name"].Value = username; 

順番に(上から下へ)、実際の属性名は次のとおりです。

  • パスワード-unicodePwd
  • フルネーム -displayName
  • AccountDisabled-userAccountControl
  • PasswordRequired- userAccountControl(実際には逆を設定します-パスワードが不要な場合のみ)
  • permPass- unicodePwd(これで何が目標だったかわからない)
于 2012-05-31T20:28:56.410 に答える
0

System.DirectoryServices.AccountManagementによって..

              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)
            {

            }
        }

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)
            {

            }
        }
于 2013-08-22T11:36:01.247 に答える