C# LDAP认证登录

作者:管理员 更新时间:2014-03-29 11:33

LDAP认证登录方式并不少见,很多公司都使用域用户登录方式,所以在开发系统的时候他们希望新系统仍然使用他们的域用户来登录,这样就需要使用到LDAP认证的登录方式。C#中已经提供的LDAP的登录方法,所以使用C#来实现LDAP认证登录就变的更加简单。

首先引用 System.DirectoryServices 类库

代码如下,直接调用即可:

public class LDAPHelper

{

string ldapUrl="LDAP://127.0.0.1/o=sa,c=org";

string ldapUserName = "cn=root,o=sa,c=org";

string ldapPassword = "root";

public LDAPHelper(string ldap_url,string ldap_user,string ldap_pwd )

{

ldapUrl = ldap_url;

ldapUserName = ldap_user;

ldapPassword = ldap_pwd;

}


public bool login()

{

DirectoryEntry root = null;

try

{

root = new DirectoryEntry(ldapUrl, ldapUserName, ldapPassword, AuthenticationTypes.None);

string strName = root.Name;//失败,会抛出异常

root.Close();

root = null;

return true;

}

catch(Exception ex)

{

return false;

}

}

}


来源:亦有软件
软件产品Eysln Software Product
亦有公告Eysln Notice
案例中心Eysln Template
知识库Eysln Knowledge Base
工具箱Eysln Toolkit Online