更新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;
}
}
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;
}
}
相关文章推荐
- 钱包农场 API 开发手记 六 更新用户信息
- 一个四用户信息同步更新的存储过程
- 小程序获取用户信息方式更新了!!
- ibatis学习之--6、根据id来更新用户信息
- DLINQ(LINQ to SQL)之用户自定义函数、在不同的DataContext之间做更新、缓存、获取信息、数据加载选项和延迟加载
- 用户登陆过程 及时更新信息方法
- 自己写的简单通过js只提供另存为功能【避免直接打开后,文件被占用,其他用户更新不了文件信息】
- ThinkPHP之用户信息查询更新和删除
- 步步为营VS 2008 + .NET 3.5(13) - DLINQ(LINQ to SQL)之用户自定义函数、在不同的DataContext之间做更新、缓存、获取信息、数据加载选项和延迟加载
- 步步为营VS 2008 + .NET 3.5(13) - DLINQ(LINQ to SQL)之用户自定义函数、在不同的DataContext之间做更新、缓存、获取信息、数据加载选项和延迟加载
- Yii2使用beforeLogout事件更新用户登录信息
- Oracle查询表创建时间,更新时间,用户信息,表类型,查询表结构等
- SharePoint 2013 数据库中手动更新用户信息
- 谷歌将整合旗下产品用户信息 3月更新服务条款
- 更新Active Directory中用户信息
- 步步为营VS 2008 + .NET 3.5(13) - DLINQ(LINQ to SQL)之用户自定义函数、在不同的DataContext之间做更新、缓存、获取信息、数据加载选项和延迟加载
- Oracle按用户进行统计信息更新
- SharePoint 2013 数据库中手动更新用户信息
- 政府必须强制winxp用户更新操作系统,否则可能出现国家灾难,迎来信息末日
- 解决-2008 R2 SP1安装Thinkpad System Update 4无法更新 提示"收集用户信息时出错"