C# LDAP获取用户信息

1.LDAP获取用户的所有信息,返回用户的字典

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Sinochem.eTravel.Bizlogics.BLL
{
    public class LDAPBLL
    {
        private static string domainName = System.Configuration.ConfigurationManager.AppSettings["LDAPAddress"];
        private static string userName = System.Configuration.ConfigurationManager.AppSettings["LDAPUser"];
        private static string userPwd = System.Configuration.ConfigurationManager.AppSettings["LDAPPwd"];

        private static string ldapService = ConfigurationManager.AppSettings["ldapService"];
        private static int ldapPort = string.IsNullOrEmpty(ConfigurationManager.AppSettings["ldapPort"]) ? 0 : Convert.ToInt32(ConfigurationManager.AppSettings["ldapPort"]);
        private static string ldapUserPatten = ConfigurationManager.AppSettings["ldapUserPatten"];

        /// <summary>
        /// 获取LDAP信息
        /// </summary>
        /// <param name="UserAccount">用户编码</param>
        /// <param name="key">uid:邮箱用户编码 employeenumber:员工编码 </param>
        /// <returns>用户结果集</returns>
        public static Dictionary<string,object> GetUser(string UserAccount,string key="uid")
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            DirectoryEntry domain = null;
            if (string.IsNullOrEmpty(UserAccount))
            {
                return dic;
            }
            try
            {
            
                if (GetCon(ref domain))
                {
                    // employeenumber // {[mobile, 
                    DirectorySearcher search = new DirectorySearcher(domain, " "+ key + "= " + UserAccount);
                    SearchResultCollection sResult = search.FindAll();

                    foreach (PropertyValueCollection item in sResult[0].GetDirectoryEntry().Properties)
                    {
                        dic.Add(item.PropertyName, item.Value);
                    }
                }
            }
            catch (Exception err)
            {
                dic.Add("error", err.Message);
            }
            return dic;
        }

        private static bool GetCon(ref DirectoryEntry domain)
        {
            string LDAP_Address = domainName;
            string LDAP_User = userName;
            string LDAP_Pwd = userPwd;
            if (domain != null)
            {
                return true;

            }
            else
            {
                domain = new DirectoryEntry();
                try
                {
                    domain.Path = LDAP_Address;
                    domain.Username = LDAP_User;
                    domain.Password = LDAP_Pwd;
                    domain.AuthenticationType = AuthenticationTypes.None;
                    domain.RefreshCache();

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}

2.app.config配置信息如下:

 <appSettings>
    <add key="ldapService" value="ldapserver.xxxxx.com" />
    <add key="dataSource" value="xxxxServer" />
    <add key="ldapPort" value="389" />
    <add key="ldapUserPatten" value="uid={0},cn=users,dc=XXX,dc=com" />
    <add key="LDAPAddress" value="LDAP://ldapserver.XXXX.com:389" />
    <add key="LDAPUser" value="uid=ldapbrowser,cn=users,dc=XXXX,dc=com" />
    <add key="LDAPPwd" value="xxxxx" />
  </appSettings>

3.查询用户信息uid为邮件编码,key传递需要查询的条件,如传递employeenumber,则按照员工编码查询相应信息

4.调用查询

  Dictionary<string, object> dic = LDAPBLL.GetUser(“[email protected]”);

如有问题,请多指教。

猜你喜欢

转载自blog.csdn.net/ying456baby/article/details/81511588
今日推荐