• 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;
            }


  • 相关阅读:
    JavaScript函数之美~
    如何用iframe标签以及Javascript制作时钟?
    Hammer.js
    leadJS初构建
    SQLite-创建、附加、分离数据库
    Javascript异步编程的4种方法
    JavaScript的数组实现队列与堆栈的方法
    使用PHP创建一个REST API(译)
    HTTP API响应数据规范整理
    CMD 模块定义规范
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2040841.html
Copyright © 2020-2023  润新知