我正在使用下面的代碼創建一個新的活動目錄用戶。該帳戶已成功創建,但是當我嘗試登錄到我的域時,我收到消息「確保您輸入了您的工作或學校帳戶的密碼」。我確保密碼輸入正確,並且帳戶在活動目錄中處於啓用和解鎖狀態。第一次設置Active Directory用戶帳戶
DirectoryEntry entry = new DirectoryEntry(createLdapPath);
try
{
DirectoryEntry newUser = entry.Children.Add("CN = " + userName, "USER");
newUser.Properties["targetAddress"].Value = "SMTP:" + userName + "@mydomain.onmicrosoft.com";
newUser.Properties["extensionAttribute15"].Value = "EDU";
newUser.Properties["proxyAddresses"].Add("SMTP:" + userName + "@mydomain1.edu");
newUser.Properties["proxyAddresses"].Add("smtp:" + userName + "@mydomain.onmicrosoft.com");
newUser.Properties["proxyAddresses"].Add("smtp:" + userName + "@mydomain2.mail.onmicrosoft.com");
newUser.Properties["givenName"].Value = fname;
newUser.Properties["sn"].Value = lname;
newUser.Properties["displayName"].Value = fname + " " + lname;
newUser.Properties["mail"].Value = fname.ToLower() + "." + lname.ToLower() + "@mydomain.edu";
newUser.Properties["sAMAccountName"].Value = fname.ToLower() + "." + lname.ToLower();
newUser.Properties["userPrincipalName"].Insert(0, fname.ToLower() + "." + lname.ToLower() + "@mydomain.edu");
newUser.CommitChanges();
newUser.Invoke("SetPassword", new object[] { "myStrongPassword" });
newUser.CommitChanges();
newUser.Close();
string strUserName = userName;
DirectoryEntry usr = entry;
DirectorySearcher searcher = new DirectorySearcher(usr);
searcher.Filter = "(SAMAccountName=" + strUserName + ")";
searcher.CacheResults = false;
SearchResult result = searcher.FindOne();
usr = result.GetDirectoryEntry();
usr.Properties["LockOutTime"].Value = 0;
int old_UAC = (int)usr.Properties["userAccountControl"][0];
// AD user account disable flag
int ADS_UF_ACCOUNTDISABLE = 2;
// To enable an ad user account, we need to clear the disable bit/flag:
usr.Properties["userAccountControl"][0] = (old_UAC & ~ADS_UF_ACCOUNTDISABLE);
usr.CommitChanges();
usr.Close();
entry.Close();
}
catch (Exception ex)
{}
當我打開「Active Directory用戶和計算機」,然後導航到我的新創建的帳戶,我只能登錄 - >右鍵 - >重置密碼。然後再次輸入密碼,並檢查「解鎖用戶」。這種方式,當我嘗試再次登錄它工作正常。
我可能在我的代碼中丟失或誤認了什麼?
謝謝你的回答,在我的情況下,事實證明,活動目錄還沒有與辦公室365同步。 – user3340627