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


  • 相关阅读:
    对万网主机全部使用独立Ip的猜疑
    对windows8在PC上的前景不看好
    漂浮广告代码3
    <!DOCTYPE> 标签一点点问题
    wordpress模板中用到的条件判断语句例1
    不错的漂浮广告代码2
    autoRST Automated TCP RST Exploit
    菜单项说明以提示弹出
    JIRA安装和破解
    谈组装对象以及对象生存期管理
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2040841.html
Copyright © 2011-2022 走看看