Windows user login validation

來源:互聯網
上載者:User
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;
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.