C#使用Domino的LDAP服务验证用户
首先,Domino的LDAP服务配置为不能匿名访问,不然就没意义了。
安全性中,internet认证设置为:较多名称变换,较低安全性。
这样的话用户名可以使用Domino个人文档中的:名+空格+姓、简称、Email地址
如果不需要的话也可以不这样设置。
密码为:Internet密码
默认情况下Domino的LDAP属性没有uid(简称),需要自己添加下。
原理:使用输入的用户名密码执行一个LDAP查询,如果没有抛出异常,说明用户名密码正确。
using System.DirectoryServices;
using System.Configuration;
using System.Text;
public class LDAPHelper
{
private string dominoServer;
/// <summary>
/// domino服务器地址
/// </summary>
public string DominoServer
{
get { return dominoServer; }
set { dominoServer = value; }
}
private string ldapServer;
/// <summary>
/// ldap服务器地址
/// </summary>
public string LdapServer
{
get { return ldapServer; }
set { ldapServer = value; }
}
private string userName;
/// <summary>
/// 用户名
/// </summary>
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string password;
/// <summary>
/// 密码
/// </summary>
public string Password
{
get { return password; }
set { password = value; }
}
/// <summary>
/// 实例化
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
public LDAPHelper(string userName, string password)
{
try
{
//this.LdapServer = ConfigurationManager.AppSettings["LDAPServer"].ToString();
//this.DominoServer = ConfigurationManager.AppSettings["DominoServer"].ToString();
this.LdapServer = "192.176.0.46";
this.DominoServer= "192.176.0.46"
this.UserName = userName;
this.Password = password;
}
catch
{
throw;
}
}
/// <summary>
/// 通过domino LDAP服务验证用户名密码是否正确
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
public bool VerifyUser()
{
try
{
DirectorySearcher searcher = prepareSearcher();
//执行以下方法时没抛出异常说明用户名密码正确
SearchResult rs = searcher.FindOne();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Prepare DirectorySearcher
/// </summary>
/// <returns>DirectorySearcher</returns>
private DirectorySearcher prepareSearcher()
{
DirectoryEntry root = new DirectoryEntry(this.LdapServer, this.UserName, this.Password, AuthenticationTypes.None);
DirectorySearcher searcher = new DirectorySearcher(root);
//LDAP搜索的过滤条件,cn为用户名,uid为用户简称
searcher.Filter = "(&(objectClass=dominoPerson)(|(cn=" + this.UserName +
")(uid=" + this.UserName + ")(mail=" + this.UserName + ")))";
//searcher.Filter = "(&(objectClass=dominoPerson)(cn=" + this.UserName +"))";
return searcher;
}
}
本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow