我需要在Active Directory中創建一個新用戶。我發現了幾個例子,如下所示:在.NET中創建Active Directory用戶(C#)
using System;
using System.DirectoryServices;
namespace test {
class Program {
static void Main(string[] args) {
try {
string path = "LDAP://OU=x,DC=y,DC=com";
string username = "johndoe";
using (DirectoryEntry ou = new DirectoryEntry(path)) {
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Add(username);
ou.CommitChanges();
}
}
catch (Exception exc) {
Console.WriteLine(exc.Message);
}
}
}
}
當我運行此代碼時,我得到沒有錯誤,但沒有創建新用戶。
我正在運行測試的帳戶具有足夠的權限在目標組織單位中創建用戶。
我是否缺少某些東西(可能是用戶對象的某些必需屬性)?
任何想法爲什麼代碼不會例外?
編輯
以下爲我工作:
int NORMAL_ACCOUNT = 0x200;
int PWD_NOTREQD = 0x20;
DirectoryEntry user = ou.Children.Add("CN=" + username, "user");
user.Properties["sAMAccountName"].Value = username;
user.Properties["userAccountControl"].Value = NORMAL_ACCOUNT | PWD_NOTREQD;
user.CommitChanges();
所以實際上有幾個問題:
CommitChanges
必須user
(感謝羅布)被稱爲- 密碼策略阻止了用戶的創建(感謝Marc)
良好的漁獲! :-) – 2009-08-19 08:25:26
閱讀「手冊」的力量:) – balexandre 2009-08-19 09:33:57