C# LDAP认证登录类参考
public class LDAPHelper
{
private DirectoryEntry _objDirectoryEntry; /// <summary>
/// 构造函数
/// </summary>
/// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param>
/// <param name="authUserName">连接用户名,例如"cn=root,dc=***,dc=com"</param>
/// <param name="authPWD">连接密码</param>
public bool OpenConnection(string LADPath, string authUserName, string authPWD)
{ //创建一个连接
_objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None); if (null == _objDirectoryEntry)
{
return false;
}
else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > )
{
return true;
}
return false;
} /// <summary>
/// 检测一个用户和密码是否正确
/// </summary>
/// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param>
/// <param name="TestUserID">testuserid</param>
/// <param name="TestUserPwd">testuserpassword</param>
/// <param name="ErrorMessage"></param>
/// <returns></returns>
public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)
{
bool blRet = false;
try
{
//创建一个检索
DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry);
//过滤名称是否存在
deSearch.Filter =strLDAPFilter;
deSearch.SearchScope = SearchScope.Subtree; //find the first instance
SearchResult objSearResult = deSearch.FindOne(); //如果用户密码为空
if (string.IsNullOrEmpty(TestUserPwd))
{
if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > )
{
blRet = true;
}
}
else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))
{
//获取用户名路径对应的用户uid
int pos = objSearResult.Path.LastIndexOf('/');
string uid = objSearResult.Path.Remove(, pos + );
DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None);
if (null != objUserEntry && objUserEntry.Properties.Count > )
{
blRet = true;
}
}
}
catch (Exception ex)
{
if (null != _objDirectoryEntry)
{
_objDirectoryEntry.Close();
}
ErrorMessage = "检测异常:"+ex.StackTrace;
}
return blRet;
} /// <summary>
/// 关闭连接
/// </summary>
public void closeConnection()
{
if (null != _objDirectoryEntry)
{
_objDirectoryEntry.Close();
}
}
}
写了一个通用的认证类,请看代码
private void btnCheck_Click(object sender, EventArgs e)
{ string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());
//deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; string TestUserID = txtUserName.Text;
string TestUserPwd = txtPwd.Text;
LDAPHelper objldap = new LDAPHelper();
string strLDAPPath = txtLDAP.Text;
string strLDAPAdminName = txtLUserName.Text;
string strLDAPAdminPwd = txtLPwd.Text;
string strMsg = "";
bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd); if (blRet)
{
blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);
if (blRet)
{
strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";
}
else if (!blRet && string.IsNullOrEmpty(strMsg))
{
strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";
}
}
this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text;
MessageBox.Show(strMsg);
}
}
public class LDAPHelper
{
private DirectoryEntry _objDirectoryEntry; /// <summary>
/// 构造函数
/// </summary>
/// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param>
/// <param name="authUserName">连接用户名,例如"cn=root,dc=***,dc=com"</param>
/// <param name="authPWD">连接密码</param>
public bool OpenConnection(string LADPath, string authUserName, string authPWD)
{ //创建一个连接
_objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None); if (null == _objDirectoryEntry)
{
return false;
}
else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > )
{
return true;
}
return false;
} /// <summary>
/// 检测一个用户和密码是否正确
/// </summary>
/// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param>
/// <param name="TestUserID">testuserid</param>
/// <param name="TestUserPwd">testuserpassword</param>
/// <param name="ErrorMessage"></param>
/// <returns></returns>
public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)
{
bool blRet = false;
try
{
//创建一个检索
DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry);
//过滤名称是否存在
deSearch.Filter =strLDAPFilter;
deSearch.SearchScope = SearchScope.Subtree; //find the first instance
SearchResult objSearResult = deSearch.FindOne(); //如果用户密码为空
if (string.IsNullOrEmpty(TestUserPwd))
{
if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > )
{
blRet = true;
}
}
else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))
{
//获取用户名路径对应的用户uid
int pos = objSearResult.Path.LastIndexOf('/');
string uid = objSearResult.Path.Remove(, pos + );
DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None);
if (null != objUserEntry && objUserEntry.Properties.Count > )
{
blRet = true;
}
}
}
catch (Exception ex)
{
if (null != _objDirectoryEntry)
{
_objDirectoryEntry.Close();
}
ErrorMessage = "检测异常:"+ex.StackTrace;
}
return blRet;
} /// <summary>
/// 关闭连接
/// </summary>
public void closeConnection()
{
if (null != _objDirectoryEntry)
{
_objDirectoryEntry.Close();
}
}
}
调用
private void btnCheck_Click(object sender, EventArgs e)
{ string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());
//deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; string TestUserID = txtUserName.Text;
string TestUserPwd = txtPwd.Text;
LDAPHelper objldap = new LDAPHelper();
string strLDAPPath = txtLDAP.Text;
string strLDAPAdminName = txtLUserName.Text;
string strLDAPAdminPwd = txtLPwd.Text;
string strMsg = "";
bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd); if (blRet)
{
blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);
if (blRet)
{
strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";
}
else if (!blRet && string.IsNullOrEmpty(strMsg))
{
strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";
}
}
this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text;
MessageBox.Show(strMsg);
}
}
实例下载:http://download.csdn.net/detail/paolei/6740833
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP。它是基于X.500标准的,但是简单多了并且可以根据需要定制。与X.500不同,LDAP支持TCP/IP,这对访问Internet是必须的。LDAP的核心规范在RFC中都有定义,所有与LDAP相关的RFC都可以在LDAPman RFC网页中找到。
bool checkResult = false;
try
{
string username = Request.Params.Get("username");
string userpwd = Request.Params.Get("userpwd");
string strLADPath = "LDAP://OU=事业部,DC=HOLD,DC=Company,DC=COM"; DirectoryEntry objEntry = new DirectoryEntry(strLADPath);
objEntry.AuthenticationType = AuthenticationTypes.None; DirectorySearcher deSearch = new DirectorySearcher(objEntry);
//过滤名称是否存在
deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))";
deSearch.SearchScope = SearchScope.Subtree;
//find the first instance
SearchResult results = deSearch.FindOne();
//check username & userpwd
if (null != results)
{
DirectoryEntry objUserEntry = new DirectoryEntry(results.Path, username, userpwd);
if (null != objUserEntry && null != objUserEntry.Properties
&& objUserEntry.Properties.Contains("cn"))
{
checkResult = true;
}
} Response.Write("认证结果:" + checkResult.ToString());
}
catch (System.Exception ex)
{
Response.Write("认证异常"+ex.StackTrace);
Response.Write("认证结果:" + checkResult.ToString());
} private void btnCheck_Click(object sender, EventArgs e)
{ string strLDAPFilter = string.Format(txtFilter.Text, txtUserName.Text.Trim());
//deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; string TestUserID = txtUserName.Text;
string TestUserPwd = txtPwd.Text;
LDAPHelper objldap = new LDAPHelper();
string strLDAPPath = txtLDAP.Text;
string strLDAPAdminName = txtLUserName.Text;
string strLDAPAdminPwd = txtLPwd.Text;
string strMsg = "";
bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd); if (blRet)
{
blRet = objldap.CheckUidAndPwd(strLDAPFilter, TestUserID, TestUserPwd, ref strMsg);
if (blRet)
{
strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";
}
else if (!blRet && string.IsNullOrEmpty(strMsg))
{
strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";
}
}
this.txtLog.Text = System.DateTime.Now.ToString() + ":" + strMsg + "\r\n" + "\r\n" + this.txtLog.Text;
MessageBox.Show(strMsg);
}
} public class LDAPHelper
{
private DirectoryEntry _objDirectoryEntry; /// <summary>
/// 构造函数
/// </summary>
/// <param name="LADPath">ldap的地址,例如"LDAP://***.***.48.110:389/dc=***,dc=com"</param>
/// <param name="authUserName">连接用户名,例如"cn=root,dc=***,dc=com"</param>
/// <param name="authPWD">连接密码</param>
public bool OpenConnection(string LADPath, string authUserName, string authPWD)
{ //创建一个连接
_objDirectoryEntry = new DirectoryEntry(LADPath, authUserName, authPWD, AuthenticationTypes.None); if (null == _objDirectoryEntry)
{
return false;
}
else if (_objDirectoryEntry.Properties!=null&&_objDirectoryEntry.Properties.Count > )
{
return true;
}
return false;
} /// <summary>
/// 检测一个用户和密码是否正确
/// </summary>
/// <param name="strLDAPFilter">(|(uid= {0})(cn={0}))</param>
/// <param name="TestUserID">testuserid</param>
/// <param name="TestUserPwd">testuserpassword</param>
/// <param name="ErrorMessage"></param>
/// <returns></returns>
public bool CheckUidAndPwd(string strLDAPFilter, string TestUserID, string TestUserPwd, ref string ErrorMessage)
{
bool blRet = false;
try
{
//创建一个检索
DirectorySearcher deSearch = new DirectorySearcher(_objDirectoryEntry);
//过滤名称是否存在
deSearch.Filter =strLDAPFilter;
deSearch.SearchScope = SearchScope.Subtree; //find the first instance
SearchResult objSearResult = deSearch.FindOne(); //如果用户密码为空
if (string.IsNullOrEmpty(TestUserPwd))
{
if (null != objSearResult && null != objSearResult.Properties && objSearResult.Properties.Count > )
{
blRet = true;
}
}
else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))
{
//获取用户名路径对应的用户uid
int pos = objSearResult.Path.LastIndexOf('/');
string uid = objSearResult.Path.Remove(, pos + );
DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, uid, TestUserPwd, AuthenticationTypes.None);
if (null != objUserEntry && objUserEntry.Properties.Count > )
{
blRet = true;
}
}
}
catch (Exception ex)
{
if (null != _objDirectoryEntry)
{
_objDirectoryEntry.Close();
}
ErrorMessage = "检测异常:"+ex.StackTrace;
}
return blRet;
} /// <summary>
/// 关闭连接
/// </summary>
public void closeConnection()
{
if (null != _objDirectoryEntry)
{
_objDirectoryEntry.Close();
}
}
}
C# LDAP认证登录类参考的更多相关文章
- C# LDAP认证登录
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据需要定制.与X ...
- Shrio00 Shiro认证登录、权限管理环境搭建
基础环境准备: JDK -> java version "1.8.0_101" MAVEN -> Apache Maven 3.5.0 1 导入依赖 mysql驱动 m ...
- 基于Thinkphp3.2的qq第三方oauth认证登录扩展类
基于Thinkphp3.2的qq第三方oauth认证登录扩展类,由于腾讯oauth sdk写的太多,不能与thinkphp和好的结合,最终想法讲腾讯oauth sdk写成tp的扩展类先看代码,将代码保 ...
- No.2 CAS之SPNEGO+LDAP认证配置
1.概述 本文先配置了SPNEGO认证,就是如果用户操作系统如果登陆了公司的Windows域,用户浏览器访问应用服务即可免登录. 然后如果不在域里的员工,用LDAP认证方式,输账号密码登陆. 参考文档 ...
- C#开发中Windows域认证登录2(扩展吉日嘎拉GPM系统)
原文地址:http://www.cuiwenyuan.com/shanghai/post/Windows-AD-Logon-Intergrated-into-Jirigala-GPM-DotNet-B ...
- centos 6.4配置samba+ldap认证
原文地址:http://www.centoscn.com/image-text/config/2015/0716/5866.html 1. 什么是samba Samba服务类似于windows上的共 ...
- 搭建harbor仓库、LDAP认证
ldap: 192.168.199.177 c5game.com 宿主机:192.168.199.224 测试客户机:192.168.199.223 安装docker.docker-compose 访 ...
- C#开发中Windows域认证登录2016(扩展吉日嘎拉GPM系统V4.2)
2013年搞公司的OA时,为了统一用户登录,将Windows AD的用户和OA的账号对接,OA用户名的规则就是使用Windows AD的用户名,格式举例:Troy.Cui,原理就是先进行域服务器的认证 ...
- 拦截器的作用之session认证登录和资源拦截
背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到springboot默认的文件夹中得文件.另外,经常需要判断当前 ...
随机推荐
- MQTT实战1 - 使用Apache Apollo代理服务器实现mqtt通信
MQTT实战1 - 使用Apache Apollo代理服务器实现mqtt通信 MQTT实战2 - 使用MQTTnet实现mqtt通信 源码下载 -> 提取码 QQ:505645074 MQTT ...
- Telegram APIs中文介绍
Telegram APIs 我们为开发者提供了两种API,Bot API (机器人API) 允许你很轻松地用Telegram的接口创建程序,Telegram API 和DLib 允许你创建定制自己的T ...
- java中方法的重载和覆盖分别要满足的条件
1.重载:遵循“两同三不同” 两同:同一个类中的同名方法 三不同:形参的类型,个数,顺序不同 特别提醒: 返回值不同构不能方法重载 形参名称不同构不成方法重载 2.覆盖(重写)的要求 子类方法的名称, ...
- 上云测试,这些关键点你get 到没有
导读,先从云化说起,再谈谈云化形态下,除了常规的功能测试,云化的测试,还需要有几个必须要get到的硬核指标,最后在分别详解这些关键点硬核指标是什么,和如何测试呢.这是个值得深思的问题,希望所有测试人都 ...
- 7. Transformer-XL原理介绍
1. 语言模型 2. Attention Is All You Need(Transformer)算法原理解析 3. ELMo算法原理解析 4. OpenAI GPT算法原理解析 5. BERT算法原 ...
- 201871010104-陈园园 《面向对象程序设计(java)》第八周学习总结
201871010104-陈园园 <面向对象程序设计(java)>第八周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 02-numpy-笔记-amin
>>> a = np.arange(4).reshape((2,2)) >>> a array([[0, 1], [2, 3]]) >>> np. ...
- python27期尚哥讲TFTP:
TFTP介绍 :TFTP(Trivial File Transfer Protocol,简单⽂件传输协议)是TCP/IP协议簇中的⼀个⽤来在客户端与服务器之间进⾏简单⽂件传输的协议使用tftp这个协议 ...
- python27期day18:模块和包、作业。
1.模块和包: 我们今天来讲解一下模块和包,模块我们已经知道是什么东西了,我们现在来看看这个包是个什么? 我说的包可不是女同胞一看见就走不动的包,而是程序中一种组织文件的形式. 只要文件夹下含有__i ...
- c#窗体之登录页(已连接数据库)
效果图: 源码: 页面: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...