예제 #1
0
        public ActionResult Index(MyClinic.Infrastructure.User user, bool Remember = false)
        {
            try
            {
                LicenseHelper.License license = new LicenseHelper.License();
                var reponseMsg = license.CheckLicenseValidated();
                userModels.LicenseMessage = "";
                //reponseMsg.IsValid = true;//Comment When Live on Production

                EntityFrameworkHelper efhelper = new EntityFrameworkHelper();
                var connectionWork             = efhelper.TestConnection();

                if (connectionWork)
                {
                    //reponseMsg.IsValid = true;
                    if (reponseMsg.IsValid)
                    {
                        if (ModelState.IsValid)
                        {
                            /*For Remember Username and Pass*/
                            if (Remember)
                            {
                                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
                                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
                            }
                            else
                            {
                                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
                            }
                            if (!String.IsNullOrEmpty(user.UserName) && !String.IsNullOrEmpty(user.Password))
                            {
                                Response.Cookies["UserName"].Value = user.UserName.Trim();
                                Response.Cookies["Password"].Value = Common.EncryptString(user.Password.Trim());
                                /*======*/

                                var logInUser = userRepository.GetUserByUsernameAndPassword(user.UserName, user.Password);
                                if (logInUser.Id > 0)
                                {
                                    if (logInUser.IsActived.Equals(1))
                                    {
                                        SessUser sessUser = new SessUser();
                                        sessUser.UserId    = logInUser.Id;
                                        sessUser.Name      = logInUser.Name;
                                        sessUser.Email     = logInUser.Email;
                                        sessUser.Username  = logInUser.UserName;
                                        sessUser.Password  = logInUser.Password;
                                        sessUser.IsActived = logInUser.IsActived;
                                        sessUser.UserType  = logInUser.UserType;
                                        sessUser.Image     = logInUser.Image;
                                        Session["user"]    = sessUser;

                                        Response.Cookies["userId"].Expires = DateTime.Now.AddDays(30);
                                        Response.Cookies["userId"].Value   = sessUser.UserId.ToString();

                                        /* For Add New Record to LogTable*/
                                        logTran.UserId      = sessUser.UserId;
                                        logTran.ProcessType = "Login";
                                        logTran.Description = "Login UserName = "******" : Name = " + sessUser.Name;
                                        logTran.LogDate     = DateTime.Now;
                                        logRepository.Add(logTran);
                                    }
                                    else
                                    {
                                        ModelState.AddModelError(string.Empty, translateSwitch.Get("You don't have authorized to access this system.​ Please contact to administrator."));
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError(string.Empty, translateSwitch.Get("Invalid username or password."));
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("", translateSwitch.Get("Please enter username & password"));
                            }
                        }
                        else
                        {
                            ModelState.AddModelError(string.Empty, translateSwitch.Get("Login was unsuccessful. Please correct the errors and try again."));
                        }
                    }
                    else
                    {
                        userModels.LicenseMessage = reponseMsg.Message;
                    }
                }
                else
                {
                    userModels.LicenseMessage = translateSwitch.Get("Cannot connect to the Database, Please contact software provider for help!");
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                ModelState.AddModelError(string.Empty, translateSwitch.Get("Invalid username or password."));
            }
            var objSession = Session["user"] as MyClinic.Infrastructure.SessUser;

            if (objSession != null)
            {
                if (Request.QueryString["rdr"] != null)
                {
                    string redirectUrl = Request.QueryString["rdr"];
                    redirectUrl = HttpUtility.UrlDecode(redirectUrl);
                    return(Redirect(redirectUrl));
                }
                else
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }
            return(View("Index", "_LayoutLogin", userModels));
        }