一个用C#操作OpenLDAP的例子

一、OpenLDAP安装和配置

LDAP目录以树状的层次结构来存储数据,最顶层即根部称作“基准DN”,形如"dc=mydomain,dc=org"或者"o=mydomain.org",OpenLDAP同时支持两种方式。我们采用前一种方式。具体安装配置过程不详述,在该过程中出现的问题一般与配置文件有关,如果出现问题请仔细检查slapd.conf文件。

二、LDAP客户端工具

非Java用户推荐使用LdapAdmin、Softerra LDAP Administrator,Java用户推荐LdapBrowser。输入用户名时的输入格式为:cn=Manager,dc=sunwin,dc=com

三、用C#操作OpenLDAP

下面代码是本人写的一个添加节点的例子,其中用到了自定义的Schema中的一个objectClass:myNodeObject。

using System.DirectoryServices.Protocols;

public class DirectoryMng
{
        private LdapConnection m_LdapConnection;
        private string m_LdapServer;
        private NetworkCredential m_Credential;
        private string m_TargetOU;

        private string ou1, ou2, ou3;

        public DirectoryMng(string ldapServer, string userName, string password, string domainName)
        {
            m_Credential = new NetworkCredential(userName, password, domainName);
            m_LdapServer = ldapServer;
            m_TargetOU = "dc=sunwin,dc=com";
        }

        public DirectoryMng(string ldapServer, string userName, string password)
        {
            m_LdapServer = ldapServer;
            m_TargetOU = "dc=sunwin,dc=com";
            userName = "cn=Manager,dc=sunwin,dc=com";
            m_Credential = new NetworkCredential(userName, password);
        }

        public void ConnectLDAP()
        {
            m_LdapConnection = new LdapConnection(m_LdapServer);
            m_LdapConnection.SessionOptions.ProtocolVersion = 3;
            m_LdapConnection.AuthType = AuthType.Basic;
            m_LdapConnection.Credential = m_Credential;
            m_LdapConnection.Bind();
            Console.WriteLine("LdapConnection is created successfully.");
        }

        public void Add()
        {
            ou1 = "myNodeID=node1," + m_TargetOU;

            DirectoryAttribute[] dirAttrList1 = new DirectoryAttribute[3];
            dirAttrList1[0] = new DirectoryAttribute("myNodeID", "node1");
            dirAttrList1[1] = new DirectoryAttribute("myNodeName", "sampleOU1");
            dirAttrList1[2] = new DirectoryAttribute("objectClass", "myNodeObject");
            AddRequest addRequest = new AddRequest(ou1, dirAttrList1);

            m_LdapConnection.SendRequest(addRequest);

            Console.WriteLine("Objects are created successfully.");
        }

}

调用方式:

static void Main(string[] args)
{
            DirectoryMng directoryMng = new DirectoryMng("192.168.20.106:389", "Manager", "admin");
            directoryMng.ConnectLDAP();
            directoryMng.Add();

}

四、自定义schema文件

一个目录中有哪些objectClass类型,一个objectClass应该包含哪些Attribute,每个Attribute具有怎样的限制,这些是在schema文件中定义的。此处的schema文件与XML schema文件相似,是数据存储的模型。objectClass和Attribute的关系:每个节点至少包含一个objectClass,这个objectClass包括多个Attribute,其中有一个名称为"objectClass”的Attribute,该Attribute用来指明该节点的是哪个objectClass的实例,如"organizationalUnit"。

下面是本人自定义的schema:

attributeType (1.1.2.1.100 NAME 'myNodeID'
        DESC '节点编号'
        EQUALITY caseIgnoreMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15
        SINGLE-VALUE )

attributeType ( 1.1.2.1.101 NAME 'myNodeName'
        DESC '节点名称'
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15)

objectclass ( 1.1.2.2.1 NAME 'myNodeObject'
        DESC 'myNode'
        SUP top STRUCTURAL
        MUST (myNodeID $ myNodeName))

简单解释一下:

  1. attributeType(...)是属性的定义,objectclass(...)是对象类的定义。
  2. 1.1.2.1.100 是对象标识符OID,可以到IANA申请免费的OID,也可以只使用1.1这个OID,只要不与现有的OID重复即可。 OID是对attribute(属性)和objectclass(对象类)的标识。
  3. NAME是属性/对象类的名称,在代码中通过该属性使用对应的属性/对象类;
  4. DESC表示该属性/对象类的描述;
  5. EQUALITY为匹配规则
  6. SYNTAX为类型标示,如1.3.6.1.4.1.1466.115.121.1.15表示字符串
  7. SINGLE-VALUE定义本属性为单值,默认为多值
  8. SUP指定上级的对象类
  9. MUST表示必须的属性

详细解释请查看相关RFC文档。

转载于:https://www.cnblogs.com/wenjingu/archive/2011/06/20/2085204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值