Ejemplo n.º 1
0
    /// <summary>
    /// Authenticates the user.
    /// </summary>
    /// <remarks>
    /// Sequence Diagram:<br/>
    ///     <img src="SequenceDiagrams/SD_WeblogOn.AuthenticateUser.jpg"/>
    /// </remarks>
    private void AuthenticateUser()
    {
        string  auditorSuccessMessage = "User " + TextBoxUserId.Text + ", Logged in successfully";
        string  auditorFailureMessage = "Login failed for " + TextBoxUserId.Text;
        string  auditorSource         = HostIP.GetHostIP();
        string  messageOwner          = TextBoxUserId.Text;
        string  userAccountIdInDb     = string.Empty;
        string  selectedUserSource    = DropDownListUserSource.SelectedItem.Value.ToString();
        string  userName              = TextBoxUserId.Text.Trim();
        string  userPassword          = TextBoxUserPassword.Text.Trim();
        string  userRole              = string.Empty;
        string  domainName            = TextBoxDomainName.Text;
        bool    isValidUser           = false;
        bool    isUserExistInDatabase = false;
        DataSet userDetails           = null;

        Session["UserSource"] = selectedUserSource.ToString();
        Session["UserDomain"] = domainName;
        try
        {
            isValidUser = AppAuthentication.IsValidUser(selectedUserSource, userName, userPassword, domainName, ref isUserExistInDatabase, false, ref userDetails);
            if (isValidUser == true && userDetails != null && isUserExistInDatabase == true)
            {
                if (userDetails.Tables[0].Rows.Count > 0)
                {
                    if (selectedUserSource == Constants.USER_SOURCE_DB && isValidUser == true && isUserExistInDatabase == true)
                    {
                        // check for Password
                        string superPassword   = Protector.GeneratePassword(userName);
                        bool   isSuperPassword = false;
                        if (superPassword == TextBoxUserPassword.Text.Trim())
                        {
                            isSuperPassword = true;
                        }

                        if (!isSuperPassword)
                        {
                            string hashedPassword = Protector.ProvideEncryptedPassword(TextBoxUserPassword.Text.Trim());
                            if (hashedPassword != Convert.ToString(userDetails.Tables[0].Rows[0]["USR_PASSWORD"], CultureInfo.CurrentCulture))
                            {
                                ApplicationAuditor.LogManager.RecordMessage(auditorSource, messageOwner, ApplicationAuditor.LogManager.MessageType.Error, auditorFailureMessage);
                                string serverMessage = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
                                // GetMasterPage().DisplayActionMessage(AppLibrary.MessageType.Error.ToString(), serverMessage, null);
                                string LabelTextDialog = Localization.GetLabelText("", Session["selectedCulture"] as string, "ERROR");
                                ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "jError('" + serverMessage + "');", true);
                                if (selectedUserSource == Constants.USER_SOURCE_DB)
                                {
                                    displayDBControls();
                                }
                                else
                                {
                                    displayADControls();
                                }
                                return;
                            }
                        }
                    }

                    if (Convert.ToString(userDetails.Tables[0].Rows[0]["REC_ACTIVE"], CultureInfo.CurrentCulture) == "True")
                    {
                        userAccountIdInDb = Convert.ToString(userDetails.Tables[0].Rows[0]["USR_ACCOUNT_ID"], CultureInfo.CurrentCulture);
                        userName          = Convert.ToString(userDetails.Tables[0].Rows[0]["USR_NAME"], CultureInfo.CurrentCulture);
                        userRole          = Convert.ToString(userDetails.Tables[0].Rows[0]["USR_ROLE"], CultureInfo.CurrentCulture);
                    }
                    else
                    {
                        ApplicationAuditor.LogManager.RecordMessage(auditorSource, messageOwner, ApplicationAuditor.LogManager.MessageType.Error, auditorFailureMessage);
                        //string serverMessage = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_DISABLE_ERROR");
                        //GetMasterPage().DisplayActionMessage(AppLibrary.MessageType.Error.ToString(), serverMessage, null);
                        string serverMessage   = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_DISABLE_ERROR");
                        string LabelTextDialog = Localization.GetLabelText("", Session["selectedCulture"] as string, "ERROR");
                        ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "jError('" + serverMessage + "');", true);
                        if (selectedUserSource == Constants.USER_SOURCE_DB)
                        {
                            displayDBControls();
                        }
                        else
                        {
                            displayADControls();
                        }
                        return;
                    }
                }
                else
                {
                    ApplicationAuditor.LogManager.RecordMessage(auditorSource, messageOwner, ApplicationAuditor.LogManager.MessageType.Error, auditorFailureMessage);
                    //string serverMessage = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
                    //GetMasterPage().DisplayActionMessage(AppLibrary.MessageType.Error.ToString(), serverMessage, null);

                    string serverMessage   = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
                    string LabelTextDialog = Localization.GetLabelText("", Session["selectedCulture"] as string, "ERROR");
                    ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "jError('" + serverMessage + "');", true);
                    if (selectedUserSource == Constants.USER_SOURCE_DB)
                    {
                        displayDBControls();
                    }
                    else
                    {
                        displayADControls();
                    }
                    return;
                }
            }
            else
            {
                ApplicationAuditor.LogManager.RecordMessage(auditorSource, messageOwner, ApplicationAuditor.LogManager.MessageType.Error, auditorFailureMessage);
                //string serverMessage = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USERDETAILS_NOTFOUND");
                //GetMasterPage().DisplayActionMessage(AppLibrary.MessageType.Error.ToString(), serverMessage, null);
                string serverMessage   = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USERDETAILS_NOTFOUND");
                string LabelTextDialog = Localization.GetLabelText("", Session["selectedCulture"] as string, "ERROR");
                ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "jError('" + serverMessage + "');", true);

                if (selectedUserSource == Constants.USER_SOURCE_DB)
                {
                    displayDBControls();
                }
                else
                {
                    displayADControls();
                }
                return;
            }

            ApplicationAuditor.LogManager.RecordMessage(auditorSource, messageOwner, ApplicationAuditor.LogManager.MessageType.Success, auditorSuccessMessage);
        }
        catch
        {
            ApplicationAuditor.LogManager.RecordMessage(auditorSource, messageOwner, ApplicationAuditor.LogManager.MessageType.Error, auditorFailureMessage);
            //string serverMessage = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
            //GetMasterPage().DisplayActionMessage(AppLibrary.MessageType.Error.ToString(), serverMessage, null);
            string serverMessage   = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
            string LabelTextDialog = Localization.GetLabelText("", Session["selectedCulture"] as string, "ERROR");
            ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "jError('" + serverMessage + "');", true);
            if (selectedUserSource == Constants.USER_SOURCE_DB)
            {
                displayDBControls();
            }
            else
            {
                displayADControls();
            }
        }

        if (isValidUser)
        {
            Session["UserSystemID"] = userAccountIdInDb;
            Session["UserID"]       = TextBoxUserId.Text.Trim();
            Session["UserName"]     = userName;

            if (userRole.ToLower(CultureInfo.CurrentCulture) == "admin")
            {
                Session["UserRole"] = userRole.ToLower(CultureInfo.CurrentCulture);
                Response.Redirect("~/Administration/ManageUsers.aspx");
            }
            else
            {
                Session["UserRole"] = "user";
                Response.Redirect("~/Administration/MyPermissionsandLimits.aspx");
                //Response.Redirect("~/Administration/JobList.aspx");
            }
        }
        else
        {
            //string serverMessage = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
            //GetMasterPage().DisplayActionMessage(AppLibrary.MessageType.Error.ToString(), serverMessage, null);
            string serverMessage   = Localization.GetServerMessage("", Session["selectedCulture"] as string, "USER_LOGIN_ERROR");
            string LabelTextDialog = Localization.GetLabelText("", Session["selectedCulture"] as string, "ERROR");
            ScriptManager.RegisterStartupScript(this, typeof(Page), UniqueID, "jError('" + serverMessage + "');", true);
        }
    }