扩大
缩小
  

LADP认证接入

一、什么是 LDAP?

LDAP 的全称是 Lightweight Directory Access Protocol(轻量目录访问协议),LDAP 「是一个协议」,约定了 Client 与 Server 之间的信息交互格式、使用的端口号、认证方式等内容。而 「LDAP 协议的实现」,有着众多版本,例如微软的 Active Directory 是 LDAP 在 Windows 上的实现,AD 实现了 LDAP 所需的树形数据库、具体如何解析请求数据并到数据库查询然后返回结果等功能。再例如 OpenLDAP 是可以运行在 Linux 上的 LDAP 协议的开源实现。而我们平常说的 LDAP Server,一般指的是安装并配置了 Active Directory、OpenLDAP 这些程序的服务器。

二、LDAP解决什么问题?

LDAP 协议能解决什么问题,那不得不提 AD。AD 是 Windows 服务器上最强大的功能,AD 是基于 LDAP 协议的一套解决方案(LDAP 服务器 + 应用),解决了细粒度的权限控制。

三、LDAP的基本模型

每一个系统、协议都会有属于自己的模型,LDAP也不例外,在了解LDAP的基本模型之前我们需要先了解几个LDAP的目录树概念:

(一)目录树概念

1. 目录树:在一个目录服务系统中,整个目录信息集可以表示为一个目录信息树,树中的每个节点是一个条目。

2. 条目:每个条目就是一条记录,每个条目有自己的唯一可区别的名称(DN)。

3. 对象类:与某个实体类型对应的一组属性,对象类是可以继承的,这样父类的必须属性也会被继承下来。

4. 属性:描述条目的某个方面的信息,一个属性由一个属性类型和一个或多个属性值组成,属性有必须属性和非必须属性。

(二)DC、UID、OU、CN、SN、DN、RDN

属性别名语法描述值(举例)
commonName cn Directory String 姓名 sean
surname sn Directory String Chow
organizationalUnitName ou Directory String 单位(部门)名称 IT_SECTION
organization o Directory String 组织(公司)名称 example
telephoneNumber   Telephone Number 电话号码 110
objectClass     内置属性 organizationalPerson

四、代码接入:

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 "";
    }
posted @ 2021-09-06 16:12  风筝遇上风  阅读(1957)  评论(0编辑  收藏  举报