我想通過C#腳本將用戶添加到活動目錄。我在網上找到了這個腳本(我自己並沒有做到)。問題是,當我嘗試添加用戶時出現此錯誤:活動目錄創建用戶
指定的目錄服務屬性或值不存在。
這是代碼,我現在所擁有的:
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;
}
}
請不要用 「C#」 和這樣的前綴您的圖書。這就是標籤的用途。 –
好的。將不會再發生,我不擅長製作標題:(。 –
請參閱http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –