History

History of .net

Monday, 4 March 2013

Check AD Authentication of the user While Login



protected void btnLogin_Click(object sender, EventArgs e)
    {
        using (HostingEnvironment.Impersonate())
        {
            string strDomain = ConfigurationManager.AppSettings["LDAPServer"];
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, strDomain))
            {
                string[] Name = txtUserName.Text.Split('\\');
                string _domainName = Name[0];
                string _userName = Name.Count() == 1 ? Name[0] : Name[1];
                // validate the credentials
                bool isValid = pc.ValidateCredentials(_userName, txtPassword.Text);
                if (isValid)
                {
                    try
                    {
                        DirectorySearcher dssearch = new DirectorySearcher(strDomain);
                        dssearch.Filter = "(SAMAccountName=" + _userName + ")";
                        SearchResult sresult = dssearch.FindOne();
                        DirectoryEntry dsresult = new DirectoryEntry();
                        dsresult = sresult.GetDirectoryEntry();

                        string sName = string.Empty, sEmailID = string.Empty;

                        if (!string.IsNullOrEmpty(dsresult.Properties["displayName"][0].ToString()))
                            sName = dsresult.Properties["displayName"][0].ToString();

                        if (dsresult.Properties["mail"].Value != null)
                            sEmailID = dsresult.Properties["mail"][0].ToString();

                        Session["STRING_SV_USERNAME"] = sName;
                        Session["STRING_SV_USER_ID"] = txtUserName.Text.Trim();
                        Session["STRING_SV_USEREMAILID"] = sEmailID;
                        Session["COMPANYID"] = ddlCompany.SelectedValue;
                        Session["COMPANY_NAME"] = ddlCompany.SelectedItem.Text;

                        Session["STRING_SV_LOGIN_STATUS"] = "Success";

                        string _companyConnStr = new CommonMethods().GetConnectionStringValueByCompanyId("1");
                        string _ShortCode = new CommonMethods().GetCompanyShortCodeByCompanyID(_companyConnStr, Convert.ToInt32(ddlCompany.SelectedValue));
                        Session["SHORTCODE"] = _ShortCode;

                        /* Code to get the authorized screens list for the logged in user */
                        objCommonMethods = new CommonMethods();

                        List<string> friendlyPageNames = objCommonMethods.GetAuthScreensListByUserIDandCompanyID(txtUserName.Text, Convert.ToInt32(ddlCompany.SelectedValue), strNewCompanyConnStr); // Change this to get the screens list

                        Session["FRIENDLY_PAGES"] = friendlyPageNames;

                        if (ddlCompany.SelectedValue != "1")
                        {
                            Session["ConfigConnectionString"] = GenerateConstr();
                        }
                        else
                        {
                            Session["ConfigConnectionString"] = ConfigurationManager.ConnectionStrings["Genpact"].ConnectionString;
                        }
                        System.Web.HttpBrowserCapabilities browser = Request.Browser;
                        Session["USER_LOGIN_ID"] = objCommonMethods.InsertUserDetails(Convert.ToString(Session["STRING_SV_USER_ID"]), browser.Browser, HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
                    }
                    catch (Exception ex)
                    {
                        Session["STRING_SV_LOGIN_STATUS"] = "Fail";
                        AlertMsg.MsgBox(Page, Resource.ERR_LOGIN);
                        txtUserName.Text = string.Empty;
                        txtPassword.Text = string.Empty;

                        objSmartViewException = new SmartViewException();
                        objSmartViewException.InsertErrorDetails(ex);
                        isValid = false;
                    }
                    if (isValid)
                    {
                        Response.Redirect("Home.aspx");
                    }
                }
                else
                {
                    Session["STRING_SV_LOGIN_STATUS"] = "Fail";
                    AlertMsg.MsgBox(Page, Resource.ERR_LOGIN);
                    txtUserName.Text = string.Empty;
                    txtPassword.Text = string.Empty;
                }
            }
        }
    }