2011年3月1日

sqlserver 與 ad 整合

假設你要ad新增一筆員工編號,sql server資料庫會跟著新增一筆,可以做一整合介面整合AD LDAP和SQLSERVER,輸入一筆使用者帳號時會做以下兩件事:
1.透過LDAP指令將資料寫入AD,
2.寫到SQLSERVER

以下是LDAP 建立account的範例:
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;
}




參考:



Howto: (Almost) Everything In Active Directory via C#

沒有留言:

張貼留言