openladp的创建和连接

2020-09-22  本文已影响0人  价值投机168

1.下载openladp:在这里下载:
http://www.userbooster.de/download/openldap-for-windows.aspx
下载完后,一路next即可(有个地方选bdb即可)。记住这个就行:

openLDAP-win-05.jpg openLDAP-win-06.jpg

2.这个完事,然后下载LdapBrowser282.
地址在:
http://www.micmiu.com/wp-content/uploads/2012/05/LdapBrowser282.zip
然后如下操作:

openLDAP-browser-01.jpg openLDAP-browser-03.jpg openLDAP-browser-04.jpg

3.在添加一项:
password为123


无标题1.png

这样添加完毕了。

4.写C#代码验证:直接拿过来就能用

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string TestUserID = "cn=newperson,ou=People,dc=maxcrc,dc=com";
        string TestUserPwd = "123";
        LDAPHelper objldap = new LDAPHelper();
        string strLDAPPath = "LDAP://127.0.0.1:389/dc=maxcrc,dc=com";
        string strLDAPAdminName = "cn=Manager,dc=maxcrc,dc=com";
        string strLDAPAdminPwd = "secret";
        string strMsg = "";
        bool blRet = objldap.OpenConnection(strLDAPPath, strLDAPAdminName, strLDAPAdminPwd);


        if (blRet)
        {
            blRet = objldap.CheckUidAndPwd("", TestUserID, TestUserPwd, ref strMsg);
            if (blRet)
            {
                strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "成功";
            }
            else if (!blRet && string.IsNullOrEmpty(strMsg))
            {
                strMsg = "检测用户名" + TestUserID + "和密码" + TestUserPwd + "失败";
            }
        }
     
        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 > 0)
        {
            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 > 0)
                {
                    blRet = true;
                }
            }
            else if (null != objSearResult && !string.IsNullOrEmpty(objSearResult.Path))
            {
                //获取用户名路径对应的用户uid
                int pos = objSearResult.Path.LastIndexOf('/');
                string uid = objSearResult.Path.Remove(0, pos + 1);
                DirectoryEntry objUserEntry = new DirectoryEntry(objSearResult.Path, TestUserID, TestUserPwd, AuthenticationTypes.None);
                if (null != objUserEntry && objUserEntry.Properties.Count > 0)
                {
                    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();
        }
    }
}

}

上一篇下一篇

猜你喜欢

热点阅读