今天写了一些LDAP 。。第一次接触LDAP。
Code
//服务器地址
private static string LDAP_HOST = "LDAP://**********";
// 管理域的dn
private static string LOGIN_DN = "cn=****,dc=*****,dc=***";
// 管理域的密码
private static string LOGIN_PASSWORD = "****";
// 基础DN
public static string Base_DN = "o=***,dc=***,dc=com";
//网络城市DN
public static string WLCS_DN = "ou=***,o=***,dc=**,dc=**";
//OO DN
public static string OO_DN = "ou=oo,o=sjtd,dc=mydomain,dc=com";
//OA DN
public static string OA_DN = "ou=oa,o=sjtd,dc=mydomain,dc=com";
/// <summary>
/// 链接LDAP
/// </summary>
DirectoryEntry entry = null;
public void LdapConnection()
{
entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
}
/// <summary>
/// 用户登录判断
/// </summary>
public string LoginLdap(string memberNo, string pwd)
{
string passport = "";
bool bl = false;
try
{
DirectoryEntry en = new DirectoryEntry(LDAP_HOST + WLCS_DN, "uid=" + memberNo + "," + WLCS_DN, pwd, AuthenticationTypes.ServerBind);
passport = en.Properties["passport"].ToString();
bl = true;
}
catch (Exception ex)
{
bl = false;
}
if (bl)
{
/// <summary>
/// 通过网络城市的UID 获得通行证,查找其他系统的用户uid
/// </summary>
DirectoryEntry en1 = new DirectoryEntry(LDAP_HOST + WLCS_DN, "uid=" + memberNo + "," + WLCS_DN, pwd, AuthenticationTypes.ServerBind);
DirectorySearcher seach = new DirectorySearcher(en1, "(uid=" + memberNo + ")");
seach.CacheResults = true;
SearchResult result = seach.FindOne();
DirectoryEntry den = result.GetDirectoryEntry();
passport = den.Properties["passport"].Value.ToString();
}
else
{ passport="";}
return passport;
}
/// <summary>
/// 增加一个新用户
/// 用户通行证生成方法:ew string((new Date()).getTime()+Math.round(Math.random()*100000)
/// </summary>
public void addUser(string memberNo ,string pwd,string netAddress)
{
Random rd = new Random();
string nember = rd.Next(100000).ToString();
string number = nember + DateTime.Now.ToString("yyyyMMddHHmm");
DirectoryEntry entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectoryEntry newuser = entry.Children.Add("uid=" + memberNo, "domain");
newuser.Properties["objectClass"].Value = "wlcsuser";
newuser.Properties["uid"].Value = memberNo;
newuser.Properties["userPassword"].Value = pwd;
newuser.Properties["passport"].Value = netAddress + number;
newuser.CommitChanges();
}
/// <summary>
/// 删除用户
/// </summary>
public void delUser(string memberNo)
{
DirectoryEntry entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectorySearcher e = new DirectorySearcher(entry, "uid=" + memberNo);
SearchResult res = e.FindOne();
DirectoryEntry del = res.GetDirectoryEntry();
entry.Children.Remove(del);
/* foreach (DirectoryEntry d in de.Children)
{
delUser(d);
}
de.DeleteTree();*/
entry.CommitChanges();
}
/// <summary>
/// 修改用户属性
/// </summary>
public void modifyUser(string memberNo, string pwd)
{
DirectoryEntry entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectorySearcher e = new DirectorySearcher(entry, "uid=" + memberNo);
SearchResult res = e.FindOne();
DirectoryEntry mod = res.GetDirectoryEntry();
mod.Properties["userPassword"].Value = pwd;
mod.Properties["passport"].Value = "12344444422323232";
mod.CommitChanges();
}
public static bool OObind(string uid, string pwd, string wlcsPossport)
{
bool bl = false;
/************************网络城市登录查询通行证**********************************/
/**************************OO查询通行证*******************************************/
string OOpassport = "";
DirectoryEntry OOen = new DirectoryEntry(LDAP_HOST + OO_DN, "uid=" + uid + "," + OO_DN, pwd, AuthenticationTypes.ServerBind);
//OO用户名 密码验证,是否合法
DirectorySearcher OOseach = new DirectorySearcher(OOen, "(uid=" + uid + ")");
OOseach.CacheResults = true;
SearchResult OOresult = OOseach.FindOne();
DirectoryEntry OOdens = OOresult.GetDirectoryEntry();
OOpassport = OOdens.Properties["passport"].Value.ToString();
//通过OO UID 查询 通行证
/***********************管理员身份进入***********************************/
DirectoryEntry en1 = new DirectoryEntry(LDAP_HOST + Base_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectorySearcher seachOO = new DirectorySearcher(en1, "passport=" + OOpassport);
DirectorySearcher seachWLCS = new DirectorySearcher(en1, "passport=" + wlcsPossport);
//查找数量 并计数
seachOO.SearchScope = SearchScope.Subtree;
seachOO.CacheResults = true;
SearchResultCollection s = seachOO.FindAll();
int i = int.Parse(s.Count.ToString());//记数
//获取网络城市 目录实体
SearchResult Wseach=seachWLCS.FindOne();
DirectoryEntry Wden=Wseach.GetDirectoryEntry();
//获取 OO的目录实体
SearchResult r = seachOO.FindOne();
DirectoryEntry OOden = r.GetDirectoryEntry();
// string bbb=OOden.Properties["passport"].Value.ToString();
if (i == 1)
{
string wpos = Wden.Properties["passport"].Value.ToString();
OOden.Properties["passport"].Value = wpos;
OOden.CommitChanges();
bl = true;
}
if (i >= 2)
{
string OOPASSPORT = OOden.Properties["passport"].Value.ToString();
Wden.Properties["passport"].Value = OOPASSPORT;
Wden.CommitChanges();
bl = true;
}
else
{
bl = false;
}
return bl;
}
写的有点乱。。。。。//服务器地址
private static string LDAP_HOST = "LDAP://**********";
// 管理域的dn
private static string LOGIN_DN = "cn=****,dc=*****,dc=***";
// 管理域的密码
private static string LOGIN_PASSWORD = "****";
// 基础DN
public static string Base_DN = "o=***,dc=***,dc=com";
//网络城市DN
public static string WLCS_DN = "ou=***,o=***,dc=**,dc=**";
//OO DN
public static string OO_DN = "ou=oo,o=sjtd,dc=mydomain,dc=com";
//OA DN
public static string OA_DN = "ou=oa,o=sjtd,dc=mydomain,dc=com";
/// <summary>
/// 链接LDAP
/// </summary>
DirectoryEntry entry = null;
public void LdapConnection()
{
entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
}
/// <summary>
/// 用户登录判断
/// </summary>
public string LoginLdap(string memberNo, string pwd)
{
string passport = "";
bool bl = false;
try
{
DirectoryEntry en = new DirectoryEntry(LDAP_HOST + WLCS_DN, "uid=" + memberNo + "," + WLCS_DN, pwd, AuthenticationTypes.ServerBind);
passport = en.Properties["passport"].ToString();
bl = true;
}
catch (Exception ex)
{
bl = false;
}
if (bl)
{
/// <summary>
/// 通过网络城市的UID 获得通行证,查找其他系统的用户uid
/// </summary>
DirectoryEntry en1 = new DirectoryEntry(LDAP_HOST + WLCS_DN, "uid=" + memberNo + "," + WLCS_DN, pwd, AuthenticationTypes.ServerBind);
DirectorySearcher seach = new DirectorySearcher(en1, "(uid=" + memberNo + ")");
seach.CacheResults = true;
SearchResult result = seach.FindOne();
DirectoryEntry den = result.GetDirectoryEntry();
passport = den.Properties["passport"].Value.ToString();
}
else
{ passport="";}
return passport;
}
/// <summary>
/// 增加一个新用户
/// 用户通行证生成方法:ew string((new Date()).getTime()+Math.round(Math.random()*100000)
/// </summary>
public void addUser(string memberNo ,string pwd,string netAddress)
{
Random rd = new Random();
string nember = rd.Next(100000).ToString();
string number = nember + DateTime.Now.ToString("yyyyMMddHHmm");
DirectoryEntry entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectoryEntry newuser = entry.Children.Add("uid=" + memberNo, "domain");
newuser.Properties["objectClass"].Value = "wlcsuser";
newuser.Properties["uid"].Value = memberNo;
newuser.Properties["userPassword"].Value = pwd;
newuser.Properties["passport"].Value = netAddress + number;
newuser.CommitChanges();
}
/// <summary>
/// 删除用户
/// </summary>
public void delUser(string memberNo)
{
DirectoryEntry entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectorySearcher e = new DirectorySearcher(entry, "uid=" + memberNo);
SearchResult res = e.FindOne();
DirectoryEntry del = res.GetDirectoryEntry();
entry.Children.Remove(del);
/* foreach (DirectoryEntry d in de.Children)
{
delUser(d);
}
de.DeleteTree();*/
entry.CommitChanges();
}
/// <summary>
/// 修改用户属性
/// </summary>
public void modifyUser(string memberNo, string pwd)
{
DirectoryEntry entry = new DirectoryEntry(LDAP_HOST + WLCS_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectorySearcher e = new DirectorySearcher(entry, "uid=" + memberNo);
SearchResult res = e.FindOne();
DirectoryEntry mod = res.GetDirectoryEntry();
mod.Properties["userPassword"].Value = pwd;
mod.Properties["passport"].Value = "12344444422323232";
mod.CommitChanges();
}
public static bool OObind(string uid, string pwd, string wlcsPossport)
{
bool bl = false;
/************************网络城市登录查询通行证**********************************/
/**************************OO查询通行证*******************************************/
string OOpassport = "";
DirectoryEntry OOen = new DirectoryEntry(LDAP_HOST + OO_DN, "uid=" + uid + "," + OO_DN, pwd, AuthenticationTypes.ServerBind);
//OO用户名 密码验证,是否合法
DirectorySearcher OOseach = new DirectorySearcher(OOen, "(uid=" + uid + ")");
OOseach.CacheResults = true;
SearchResult OOresult = OOseach.FindOne();
DirectoryEntry OOdens = OOresult.GetDirectoryEntry();
OOpassport = OOdens.Properties["passport"].Value.ToString();
//通过OO UID 查询 通行证
/***********************管理员身份进入***********************************/
DirectoryEntry en1 = new DirectoryEntry(LDAP_HOST + Base_DN, LOGIN_DN, LOGIN_PASSWORD, AuthenticationTypes.ServerBind);
DirectorySearcher seachOO = new DirectorySearcher(en1, "passport=" + OOpassport);
DirectorySearcher seachWLCS = new DirectorySearcher(en1, "passport=" + wlcsPossport);
//查找数量 并计数
seachOO.SearchScope = SearchScope.Subtree;
seachOO.CacheResults = true;
SearchResultCollection s = seachOO.FindAll();
int i = int.Parse(s.Count.ToString());//记数
//获取网络城市 目录实体
SearchResult Wseach=seachWLCS.FindOne();
DirectoryEntry Wden=Wseach.GetDirectoryEntry();
//获取 OO的目录实体
SearchResult r = seachOO.FindOne();
DirectoryEntry OOden = r.GetDirectoryEntry();
// string bbb=OOden.Properties["passport"].Value.ToString();
if (i == 1)
{
string wpos = Wden.Properties["passport"].Value.ToString();
OOden.Properties["passport"].Value = wpos;
OOden.CommitChanges();
bl = true;
}
if (i >= 2)
{
string OOPASSPORT = OOden.Properties["passport"].Value.ToString();
Wden.Properties["passport"].Value = OOPASSPORT;
Wden.CommitChanges();
bl = true;
}
else
{
bl = false;
}
return bl;
}
具体的 还在实现当中~~~