zoukankan      html  css  js  c++  java
  • Windows user login validation

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.DirectoryServices;

    namespace VQP.BLL
    {
        public class LdapAuthentication
        {
            private string _path;
            private string _filterAttribute = string.Empty;

            public LdapAuthentication()
            {
                //
                // TODO: Add constructor logic here
                //
            }

            /// <summary>
            ///
            /// </summary>
            /// <param name="path"></param>
            public LdapAuthentication(string path)
            {
                _path = path;
            }


            /// <summary>
            ///
            /// </summary>
            /// <param name="domain"></param>
            /// <param name="username"></param>
            /// <param name="pwd"></param>
            /// <returns></returns>
            public bool IsAuthenticated(string username, string
                pwd)
            {
                return IsAuthenticated(false, "", username, pwd);
            }

            /// <summary>
            ///
            /// </summary>
            /// <param name="domain"></param>
            /// <param name="username"></param>
            /// <param name="pwd"></param>
            /// <returns></returns>
            public bool IsAuthenticated(bool includeDomain, string domain, string username, string pwd)
            {
                bool bResult = false;

                string domainAndUsername = username;
                if (includeDomain)
                    domainAndUsername = domain + @"\" + username;

                DirectoryEntry entry = new DirectoryEntry(_path,
                    domainAndUsername,
                    pwd);

                try
                {
                    // Bind to the native AdsObject to force authentication.
                    Object obj = entry.NativeObject;
                    bResult = true;

                    //                DirectorySearcher search = new DirectorySearcher(entry);
                    //
                    //                search.Filter = "(SAMAccountName=" + username + ")";
                    //
                    //                search.PropertiesToLoad.Add("cn");
                    //               
                    //                SearchResult result = search.FindOne();
                    //
                    //                // Update the new path to the user in the directory
                    //                if ( result != null)
                    //                {
                    //                    _path = result.Path;
                    //                   
                    //                    _filterAttribute = (String)result.Properties["cn"][0];
                    //                   
                    //                    bResult = true;
                    //                }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error authenticating user. " + ex.ToString());
                }
                return bResult;
            }

            public string GetGroups()
            {
                DirectorySearcher search = new DirectorySearcher(_path);
                search.Filter = "(cn=" + _filterAttribute + ")";
                search.PropertiesToLoad.Add("memberOf");
                StringBuilder groupNames = new StringBuilder();

                try
                {
                    SearchResult result = search.FindOne();
                    int propertyCount = result.Properties["memberOf"].Count;
                    string dn;
                    int equalsIndex, commaIndex;

                    for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
                    {
                        dn = (string)result.Properties["memberOf"][propertyCounter];
                        equalsIndex = dn.IndexOf("=", 1);
                        commaIndex = dn.IndexOf(",", 1);
                        if (-1 == equalsIndex)
                        {
                            return null;
                        }
                        groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                        groupNames.Append("|");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Error obtaining group names. " + ex.Message);
                }
                return groupNames.ToString();
            }

        }
    }


     /// <summary>
            ///
            /// </summary>
            /// <param name="strUserID"></param>
            /// <param name="strPassword"></param>
            /// <returns></returns>
            public string ValidUserLogin( string strUserID, string strPassword)
            {
                string strMessage = string.Empty;


                // CORP user has 8 characters, MATPARTNERS user must not be 8 characters.  
                string strPath = "LDAP://MATPARTNERS";
                if (strUserID.Length == 8)
                    strPath = "LDAP://CORP";

                string domain = "MATPARTNERS";
                if (strUserID.Length == 8)
                    domain = "CORP";
                try
                {
                    LdapAuthentication objBLL = new LdapAuthentication(strPath);
                    if (!objBLL.IsAuthenticated(true, domain, strUserID, strPassword))
                    {
                        strMessage += "Please check your name or password!";
                    }
                }
                catch (Exception ex)
                {
                    strMessage += "Please check your name or password!";
                }

                return strMessage;
            }


  • 相关阅读:
    【JZOJ4616】二进制的世界
    【JZOJ4665】数列
    【JZOJ4811】排队
    2017.08.19【NOIP提高组】模拟赛B组 经济编码
    浅谈匈牙利算法
    2017.08.18【NOIP提高组】模拟赛B组 恭介的法则(rule)
    2017.08.18【NOIP提高组】模拟赛B组 沙耶的玩偶(doll)
    2017.08.15【NOIP提高组】模拟赛B组 单足跳
    2017.08.15【NOIP提高组】模拟赛B组 生日聚餐
    2017.08.12【NOIP提高组】模拟赛B组 巴比伦
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2040841.html
Copyright © 2011-2022 走看看