您的位置:首页 > 其它

更新Active Directory中用户信息

2010-02-17 17:33 92 查看
首先根据UserName创建DirectoryEntry对象实例:
DirectoryEntry de= new DirectoryEntry(results.Path,ADUser,ADPassword,AuthenticationTypes.Secure);
需要注意的是ADUser/ADPassword必须具有Account Operator或Administrator的权限,否则de.CommitChanges();会抛出异常。

下面的示例代码演示从DataSet中获取AD属性值,并赋予给对应的AD属性。同时,也演示了如何使用AD的扩展属性extensionAttribute1 -extensionAttribute6:
public static void UpdateUserByDataSet(DataSet dsUser)
{
string UserName = dsUser.Tables[0].Rows[0]["LoginName"].ToString();
DataRow theRow = dsUser.Tables[0].Rows[0];
DirectoryEntry deUser = GetUser(UserName);

if(theRow["FirstName"].ToString().Trim().Length != 0)
deUser.Properties["givenName"].Value = theRow["FirstName"].ToString();
if(theRow["MiddleInitial"].ToString().Trim().Length != 0)
deUser.Properties["initials"].Value = theRow["MiddleInitial"].ToString();
if(theRow["LastName"].ToString().Trim().Length != 0)
deUser.Properties["sn"].Value = theRow["LastName"].ToString();
if(theRow["Alias"].ToString().Trim().Length != 0)
deUser.Properties["mailNickname"].Value = theRow["Alias"].ToString();
if(theRow["Display"].ToString().Trim().Length != 0)
deUser.Properties["displayName"].Value = theRow["Display"].ToString();
if(theRow["Title"].ToString().Trim().Length != 0)
deUser.Properties["Title"].Value = theRow["Title"].ToString();

if(theRow["Address"].ToString().Trim().Length != 0)
deUser.Properties["streetAddress"].Value = theRow["Address"].ToString();
if(theRow["Company"].ToString().Trim().Length != 0)
deUser.Properties["company"].Value = theRow["Company"].ToString();
if(theRow["Department"].ToString().Trim().Length != 0)
deUser.Properties["department"].Value = theRow["Department"].ToString();
if(theRow["Office"].ToString().Trim().Length != 0)
deUser.Properties["physicalDeliveryOfficeName"].Value = theRow["Office"].ToString();
if(deUser.Properties["Assistant"].ToString().Trim().Length != 0)
deUser.Properties["telephoneAssistant"].Value = theRow["Assistant"].ToString();

if(theRow["City"].ToString().Trim().Length != 0)
deUser.Properties["l"].Value = theRow["City"].ToString();
if(theRow["State"].ToString().Trim().Length != 0)
deUser.Properties["st"].Value = theRow["State"].ToString();
if(theRow["Zip"].ToString().Trim().Length != 0)
deUser.Properties["postalCode"].Value = theRow["Zip"].ToString();
if(theRow["Country"].ToString().Trim().Length != 0)
deUser.Properties["co"].Value = theRow["Country"].ToString();

// Phone & Notes
…………

// Job Data
if(theRow["AdminSupervisor"].ToString().Trim().Length != 0)
deUser.Properties["extensionAttribute1"].Value = theRow["AdminSupervisor"].ToString();
if(theRow["AdminSubordinates"].ToString().Trim().Length != 0)
deUser.Properties["extensionAttribute2"].Value = theRow["AdminSubordinates"].ToString();
if(theRow["AdminDelegates"].ToString().Trim().Length != 0)
deUser.Properties["extensionAttribute3"].Value = theRow["AdminDelegates"].ToString();
if(theRow["FunctionalSupervisor"].ToString().Trim().Length != 0)
deUser.Properties["extensionAttribute4"].Value = theRow["FunctionalSupervisor"].ToString();
if(theRow["FunctionalSubordinates"].ToString().Trim().Length != 0)
deUser.Properties["extensionAttribute5"].Value = theRow["FunctionalSubordinates"].ToString();
if(theRow["FunctionalDelegates"].ToString().Trim().Length != 0)
deUser.Properties["extensionAttribute6"].Value = theRow["FunctionalDelegates"].ToString();

deUser.CommitChanges();
}

创建DirectoryEntry对象实例:
/// <summary>
/// This will return a DirectoryEntry object if the user does exist
/// </summary>
/// <param name="UserName"></param>
/// <returns></returns>
public static DirectoryEntry GetUser(string UserName)
{
//create an instance of the DirectoryEntry
DirectoryEntry de = GetDirectoryObject();

//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher();

deSearch.SearchRoot =de;
//set the search filter
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))";
deSearch.SearchScope = SearchScope.Subtree;

//find the first instance
SearchResult results= deSearch.FindOne();

//if found then return, otherwise return Null
if(results !=null)
{
de= new DirectoryEntry(results.Path,ADUser,ADPassword,AuthenticationTypes.Secure);
//if so then return the DirectoryEntry object
return de;
}
else
{
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐