C#用户进行LDAP验证并返回员工信息
2013-07-26 13:53
1436 查看
目前多数公司都用windows域管理用户和电脑,
因此在内部应用中,使用LDAP进行用户验证,并返回LDAP的用户信息,如员工工号就十分有意义,
以下是一段关键代码,对LDAP账号密码验证后,返回员工号,完整的例子见附件
注意要引用以下namespace
using System.DirectoryServices;
private static string GetEmpIDFromLDAP(string UserName, string password)
{
//return true;
DirectoryEntry AD = new DirectoryEntry("LDAP://RootDSE");
String str = AD.Properties["defaultNamingContext"][0].ToString();
AD.Path = "LDAP://" + str;
AD.Username = UserName;
AD.Password = password;
AD.AuthenticationType = AuthenticationTypes.Secure;
try
{
DirectorySearcher searcher = new DirectorySearcher(AD);
searcher.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", UserName);
System.DirectoryServices.SearchResult result = searcher.FindOne();
if (result != null)
{
string empid = result.Properties["employeenumber"][0].ToString();
return empid;
}
else
{
return "";
}
}
catch (Exception err)
{
string a = err.Message;
}
return "";
}
本文出自 “宋超技术博客” 博客,请务必保留此出处http://soungcha.blog.51cto.com/1075631/1258091
因此在内部应用中,使用LDAP进行用户验证,并返回LDAP的用户信息,如员工工号就十分有意义,
以下是一段关键代码,对LDAP账号密码验证后,返回员工号,完整的例子见附件
注意要引用以下namespace
using System.DirectoryServices;
private static string GetEmpIDFromLDAP(string UserName, string password)
{
//return true;
DirectoryEntry AD = new DirectoryEntry("LDAP://RootDSE");
String str = AD.Properties["defaultNamingContext"][0].ToString();
AD.Path = "LDAP://" + str;
AD.Username = UserName;
AD.Password = password;
AD.AuthenticationType = AuthenticationTypes.Secure;
try
{
DirectorySearcher searcher = new DirectorySearcher(AD);
searcher.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", UserName);
System.DirectoryServices.SearchResult result = searcher.FindOne();
if (result != null)
{
string empid = result.Properties["employeenumber"][0].ToString();
return empid;
}
else
{
return "";
}
}
catch (Exception err)
{
string a = err.Message;
}
return "";
}
本文出自 “宋超技术博客” 博客,请务必保留此出处http://soungcha.blog.51cto.com/1075631/1258091
相关文章推荐
- csrf攻击过程 csrf攻击说明 1.用户C打开浏览器,访问受信任网站A,输入用户名和密码请求登录网站A; 2.在用户信息通过验证后,网站A产生Cookie信息并返回给浏览器,此时用户登录网站
- C#使用Domino的LDAP服务验证用户
- asp.ne如何使用javascript去验证客户端信息,如果验证成功则送往服务器端处理,否则在客户端提示用户(不返回到服务器端处理)
- C#使用Domino的LDAP服务验证用户
- 在C#中进行数据纵向不定行转横向列,多条信息成一行,例如员工薪资信息
- C#Ldap查找用户信息
- 用户管理,信息验证组件 ----ldap的配置
- C#验证用户输入信息是否包含危险字符串的方法
- 使用LDAP/SSL进行RequisitePro用户验证及管理
- 1 完成登录页面(到记事本文件进行密码和用户名的验证)。 2 完成 用户注册页面(将注册信息显示在本页面 div 中)。
- ldap验证用户(java)<不能以simple协议来获取连接进行连接>
- C#实现将选中复选框的信息返回给用户的方法
- 使用LDAP/SSL进行RequisitePro用户验证及管理
- C# 数据库 检查用户信息 验证登陆 十四章
- Java程序通过LDAP对用户进行登陆验证
- PHP从入门到精通 例6.6 应用正则表达式对用户注册信息进行验证
- C#使用Domino的LDAP服务验证用户
- C# 调用LDAP接口获取域用户信息
- [C#]VS2010 连接数据库进行用户验证和注册(学习笔记)
- 使用jQuery插件jRemoteValidate进行远程ajax验证,可以自定义返回的信息