protected void btnLogIn_Click(object sender, EventArgs e)
        {
            try
            {

                Users user = new Users().GetUserByUserName(txtUserName.Value);
                if (user.UserId != 0)
                {
                    if (user.UserPassword != txtPassword.Value)
                    {
                        Alert.Show("User and password didn't match. Please re-enter the correct password.");
                        txtPassword.Focus();
                        return;
                    }

                    int companyId = int.Parse(ddlCompany.SelectedValue);

                    Session["user"] = user;
                    UserRoleMapping userRole = new UserRoleMapping().GetUserRoleMappingByUserId(user.UserId);
                    UserRole role = new UserRole().GetUserRoleById(userRole.RoleId, companyId);
                    Session["Role"] = role;

                    //Get host and port from the url;
                    string host = HttpContext.Current.Request.Url.Host;
                    string port = HttpContext.Current.Request.Url.Port.ToString();

                    string path = "http://" + host + ":" + port + "/";
                    this.GenerateMenu(user, path, Int32.Parse("0"));

                    Company company= new Company().GetCompanyByCompanyId(companyId);
                    Session["company"] = company;

                    if (user.EmployeeId != 0)
                    {
                        Employee employee = new Employee().GetEmployeeByEmployeeId(user.EmployeeId,
                            user.CompanyId);
                        Session["Employee"] = employee;

                        //Department objDepartment = new Department().GetEmployeeDepartment(user.EmployeeId);
                        //Session["Department"] = objDepartment.DepartmentName;
                    }
                    else
                        Session["Department"] = "All";

                    string refPage = (Request.QueryString["refPage"] == null) ? string.Empty : Request.QueryString["refPage"].ToString();
                    Response.Redirect(((refPage == string.Empty || refPage.ToLower() == "logout") ? "index.aspx" : refPage), false);
                }
                else
                {
                    Alert.Show("The user is not exist in the database. Please check the username.");
                    txtUserName.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                Alert.Show("Error during process user authentication. Error: " + ex.Message);
            }
        }
Exemple #2
0
        protected void btnLogIn_Click(object sender, EventArgs e)
        {
            try
            {
                Users user = new Users().GetUserByUserName(txtUserName.Value);
                if (user.UserId != 0)
                {
                    if (user.UserPassword != txtPassword.Value)
                    {
                        Alert.Show("User and password didn't match. Please re-enter the correct password.");
                        txtPassword.Focus();
                        return;
                    }

                    //string IP = Request.UserHostName;
                    //string compName = DetermineCompName(IP);

                    //UserLoginLog log = new UserLoginLog().GetUserLastLogin(user.UserId);
                    //if (log.Id != 0)
                    //{
                    //    if (log.IpAddress != IP && log.Status == "Logged In")
                    //    {
                    //        Alert.Show("Sorry! This is user is already logged in from another PC.");
                    //        return;
                    //    }
                    //}

                    Session["user"] = user;
                    UserRoleMapping userRole = new UserRoleMapping().GetUserRoleMappingByUserId(user.UserId);
                    UserRole role = new UserRole().GetUserRoleById(userRole.RoleId, user.CompanyId);
                    Session["Role"] = role;

                    //Get host and port from the url;
                    string host = HttpContext.Current.Request.Url.Host;
                    string port = HttpContext.Current.Request.Url.Port.ToString();

                    string path = "http://" + host + ":" + port + "/";
                    this.GenerateMenu(user, path);

                    //log = new UserLoginLog();
                    //log.UserId = user.UserId;
                    //log.SessionId = Session.SessionID;

                    //log.IpAddress = IP;
                    //log.LoginPCName = compName;
                    //log.LoginTime = DateTime.Now;
                    //log.Status = "Logged In";
                    //log.LogOutTime = PublicVariables.minDate;

                    //log.InsertUserLoginLog();
                    Company company;
                    UserRoleMapping userRoles = new UserRoleMapping().GetUserRoleMappingByUserId(user.UserId);
                    if (userRoles.RoleId != 0 && user.UserId == 1)
                    {
                        user.IsSuperUser = true;
                        company = new Company().GetCompanyByCompanyId(1);
                    }
                    else
                    {
                        user.IsSuperUser = false;
                        company = new Company().GetCompanyByCompanyId(user.CompanyId);
                    }

                    Session["company"] = company;

                    if (user.CompanyId == 0 && !user.IsSuperUser)
                    {
                        Alert.Show("Sorry this user is not associated with any company. Contact your system administrator to fix this issue.");
                        return;
                    }

                    if (user.EmployeeId != 0)
                    {
                        Employee employee = new Employee().GetEmployeeByEmployeeId(user.EmployeeId,
                            user.CompanyId);
                        Session["Employee"] = employee;

                        //Department objDepartment = new Department().GetEmployeeDepartment(user.EmployeeId);
                        //Session["Department"] = objDepartment.DepartmentName;
                    }
                    else
                        Session["Department"] = "All";

                    string refPage = (Request.QueryString["refPage"] == null) ? string.Empty : Request.QueryString["refPage"].ToString();
                    Response.Redirect(((refPage == string.Empty || refPage.ToLower() == "logout") ? "index.aspx" : refPage), false);
                }
                else
                {
                    Alert.Show("The user is not exist in the database. Please check the username.");
                    txtUserName.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                Alert.Show("Error during process user authentication. Error: " + ex.Message);
            }
        }