码迷,mamicode.com
首页 > 其他好文 > 详细

AD域验证DirectoryEntry用法

时间:2015-01-08 14:52:23      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

引用:

using System.Configuration;
using System.DirectoryServices;

web.config加配置:把下面的ip改为你自己域服务器ip

<appSettings>
    <!--域登陆Path-->
    <add key="ADPath" value="LDAP:10.10.1.111"/>
    <!--域登陆Domain-->
    <add key="ADDomain" value="10.10.1.111"/>
  </appSettings>

 

/// <summary>
        /// 域账号是否登陆成功
        /// </summary>
        /// <param name="username">域登陆账号</param>
        /// <param name="pwd">域登陆密码</param>
        /// <returns></returns>
        public static bool IsAuthenticated(string username, string pwd)
        {
            string adPath = ConfigurationManager.AppSettings["ADPath"].ToString();
            string domain = ConfigurationManager.AppSettings["ADDomain"].ToString();
            string domainUserName = domain + @"\" + username;      //或者可以这样 string domainUserName = username;
            DirectoryEntry entry = new DirectoryEntry(adPath, domainUserName, pwd);
            try
            {
                DirectorySearcher deSearch = new DirectorySearcher(entry);
                deSearch.Filter = "(&(objectCategory=Person)(objectClass=User)(SAMAccountName=" + username + "))";
                deSearch.PropertiesToLoad.Add("cn");
                SearchResult result = deSearch.FindOne();
                if (null == result)
                {
                    return false;
                }
                //可以获取相关信息
                string _path = result.Path;
                string _filterAttribute = (string)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return true;
        }

 

AD域验证DirectoryEntry用法

标签:

原文地址:http://www.cnblogs.com/WsxSuper/p/4210748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!