I have code Login with remember me code.it's me code login is run but remember me not run .Please check this code.
- [HttpPost]
- [AllowAnonymous]
- public async Task<ActionResult> Login(UserLoginViewModel model)
- {
- try
- {
- if (ModelState.IsValid)
- {
- var userdetails = await _context.UserMaster
- .SingleOrDefaultAsync(m => m.Email == model.Email && m.Password == model.Password);
- if (userdetails == null)
- {
- ModelState.AddModelError("", "Invalid login attempt.");
- return View("Login");
- }
- if (userdetails != null)
- {
- if (userdetails.UserRoleId == 1)
- {
- var userClaims = new List<Claim>()
- {
- new Claim(ClaimTypes.Name,userdetails.Password),
- new Claim(ClaimTypes.Email, userdetails.Email),
- };
- var grandmaIdentity = new ClaimsIdentity(userClaims, "UserIdentity");
- var userPrincipal = new ClaimsPrincipal(new[] { grandmaIdentity });
- await HttpContext.SignInAsync(userPrincipal,
- new AuthenticationProperties
- {
- IsPersistent = model.RememberMe,
- ExpiresUtc = DateTime.Now.AddHours(1)
- });
- HttpContext.Session.SetString("AdminName", userdetails.FullName);
- HttpContext.Session.SetString("AdminImage", userdetails.AdminImage);
- string a = HttpContext.Session.GetString("AdminName");
- string b = HttpContext.Session.GetString("AdminImage");
- }
- }
- }
- else
- {
- ModelState.AddModelError(string.Empty, "The user name or password is incorrect");
- return View();
- }
- return RedirectToAction("Dashboard", "Dashboard");
- }
- catch (Exception ex)
- {
- ViewBag.message = "Internal Server Error";
- var message = ex.InnerException.Message;
- throw;
- }
- }
- [HttpPost]
- public IActionResult Logout()
- {
- HttpContext.SignOutAsync(
- CookieAuthenticationDefaults.AuthenticationScheme);
- return RedirectToAction("Login", "Login");
- }
- }