域帐号查询与更新

域帐号查询与更新

 引用

<%@ Import Namespace=”System.DirectoryServices” %>

private bool isLoginNameExist(string ln)
{
DirectoryEntry entry = new DirectoryEntry(ldap);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
// Set DerefAlias to Always.
mySearcher.DerefAlias = DereferenceAlias.Always;
//Use the FindAll method to get search results.
SearchResultCollection res = mySearcher.FindAll();
foreach (SearchResult result in res)
{
ResultPropertyCollection props = result.Properties;
if (props.Contains(”samaccountname”) == true)
if ((”ceapet.com""” + props["samaccountname"][0].ToString()) == ln)
{
mySearcher.Dispose();
entry.Dispose();
return true;
}
}
return false;
}

protected ArrayList readAD()
{
ArrayList adlist = new ArrayList();
try
{
DirectoryEntry entry = new DirectoryEntry(ldap);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
// Set DerefAlias to Always.
mySearcher.DerefAlias = DereferenceAlias.Always;
//Use the FindAll method to get search results.
SearchResultCollection res = mySearcher.FindAll();

foreach (SearchResult result in res)
{
ResultPropertyCollection props = result.Properties;
if (props.Contains(”samaccountname”) == true && props.Contains(”adspath”) == true)
adlist.Add(props["adspath"][0].ToString());
}
mySearcher.Dispose();
entry.Dispose();
}
catch (Exception ex)
{
w(ex.ToString());
}
return adlist;

}

protected void updateAD(ArrayList adlist, string username)
{
try
{
ArrayList userinfo;
DirectoryEntry entry;
for (int i = 0; i < adlist.Count; i++)
{
entry = new DirectoryEntry(adlist[i].ToString(), “username”, “password”);
entry.AuthenticationType = AuthenticationTypes.Secure;
if (entry.Properties.Contains(”samaccountname”) == true)
{
if (”ceapet.com""” + entry.Properties["samaccountname"][0].ToString().ToLower() == username.ToLower())
{
userinfo = new ArrayList();
userinfo = getUserByloginName(”ceapet.com""” + entry.Properties["samaccountname"][0].ToString());
if (userinfo.Count == 6)
{
if (entry.Properties.Contains(”name”) == true)
{
//entry.Properties["name"].Value = userinfo[1].ToString();
//entry.CommitChanges();
}
if (entry.Properties.Contains(”displayName”) == true)
{
entry.Properties["displayName"].Value = userinfo[1].ToString();
entry.CommitChanges();
}
if (entry.Properties.Contains(”department”) == true)
{
entry.Properties["department"].Value = userinfo[2].ToString();
entry.CommitChanges();
}

if (entry.Properties.Contains(”title”) == true)
{
entry.Properties["title"].Value = userinfo[3].ToString();
entry.CommitChanges();
}
if (entry.Properties.Contains(”mail”) == true)
{
entry.Properties["mail"].Value = userinfo[4].ToString();
entry.CommitChanges();
}
if (entry.Properties.Contains(”telephoneNumber”) == true)
{
entry.Properties["telephoneNumber"].Value = userinfo[5].ToString();
entry.CommitChanges();
}
}
}
}
entry.Dispose();
}
}
catch (Exception ex)
{
w(ex.ToString());
}
}

 源代码参考页:

 http://ceapet.com/blog/?p=344

原文地址:https://www.cnblogs.com/liuzhengdao/p/1290078.html