Example #1
0
        public IActionResult Login(string returnUrl = "/")
        {
            if (HttpContext.Session.GetString("uid_a") != null && HttpContext.Session.GetString("mhk") == "admin")
            {
                return(Redirect("/Admin/Index"));
            }
            string sel = Request.Cookies["ils"];
            string val = Request.Cookies["ajv"];

            if (sel != null && val != null)
            {
                int?userId = UserManager.VerifyAuthCredentials(sel, val);
                if (userId != null)
                {
                    HttpContext.Session.SetString("uid_a", userId.ToString());
                    HttpContext.Session.SetString("mhk", "admin");
                    string token = UserManager.GenerateToken();
                    UserManager.SetAuthCredentials(sel, token, (int)userId, DateTime.Now.AddMinutes(14400));

                    CookieExtensions.SetCookie("ajv", token, 14400, Response, true, true);
                    return(Redirect("/Admin/Index"));
                }
            }

            return(View(new LoginViewModel
            {
                ReturnUrl = returnUrl
            }));
        }
Example #2
0
        public IActionResult Login(LoginViewModel LoginModel)
        {
            if (HttpContext.Session.GetString("uid_a") != null && HttpContext.Session.GetString("mhk") == "admin")
            {
                return(Redirect("/Admin/Index"));
            }
            if (ModelState.IsValid)
            {
                User user = UserManager.FindUser(LoginModel.Username, LoginModel.Password);
                if (user != null)
                {
                    foreach (var i in user.Roles)
                    {
                        if (i != null && i.Name == "Admin")
                        {
                            HttpContext.Session.SetString("mhk", "admin");
                            break;
                        }
                    }
                    HttpContext.Session.SetString("uid_a", user.UserId.ToString());
                    user.isLoggedIn = true;
                    if (LoginModel.Rememberme)
                    {
                        string selector = UserManager.GenerateSelector();
                        string token    = UserManager.GenerateToken();
                        UserManager.SetAuthCredentials(selector, token, (int)user.UserId, DateTime.Now.AddMinutes(14400));

                        CookieExtensions.SetCookie("ils", selector, 14400, Response, true, true);
                        CookieExtensions.SetCookie("ajv", token, 14400, Response, true, true);
                    }
                    return(Redirect(LoginModel?.ReturnUrl ?? "/Admin/Index"));
                }

                TempData["message"] = "Oops! Looks like you typed it wrong.";
            }
            return(View(LoginModel));
        }