Guest User

Guest User

  • Tech Writer
  • 611
  • 129.3k

Remember me in .net core.

Sep 3 2020 1:27 AM
I have code Login with remember me code.it's me code login is run but remember me not run .Please check this code.
  1. [HttpPost]  
  2. [AllowAnonymous]  
  3. public async Task<ActionResult> Login(UserLoginViewModel model)  
  4. {  
  5. try  
  6. {  
  7. if (ModelState.IsValid)  
  8. {  
  9. var userdetails = await _context.UserMaster  
  10. .SingleOrDefaultAsync(m => m.Email == model.Email && m.Password == model.Password);  
  11. if (userdetails == null)  
  12. {  
  13. ModelState.AddModelError("""Invalid login attempt.");  
  14. return View("Login");  
  15. }  
  16. if (userdetails != null)  
  17. {  
  18. if (userdetails.UserRoleId == 1)  
  19. {  
  20. var userClaims = new List<Claim>()  
  21. {  
  22. new Claim(ClaimTypes.Name,userdetails.Password),  
  23. new Claim(ClaimTypes.Email, userdetails.Email),  
  24. };  
  25. var grandmaIdentity = new ClaimsIdentity(userClaims, "UserIdentity");  
  26. var userPrincipal = new ClaimsPrincipal(new[] { grandmaIdentity });  
  27. await HttpContext.SignInAsync(userPrincipal,  
  28. new AuthenticationProperties  
  29. {  
  30. IsPersistent = model.RememberMe,  
  31. ExpiresUtc = DateTime.Now.AddHours(1)  
  32. });  
  33. HttpContext.Session.SetString("AdminName", userdetails.FullName);  
  34. HttpContext.Session.SetString("AdminImage", userdetails.AdminImage);  
  35. string a = HttpContext.Session.GetString("AdminName");  
  36. string b = HttpContext.Session.GetString("AdminImage");  
  37. }  
  38. }  
  39. }  
  40. else  
  41. {  
  42. ModelState.AddModelError(string.Empty, "The user name or password is incorrect");  
  43. return View();  
  44. }  
  45. return RedirectToAction("Dashboard""Dashboard");  
  46. }  
  47. catch (Exception ex)  
  48. {  
  49. ViewBag.message = "Internal Server Error";  
  50. var message = ex.InnerException.Message;  
  51. throw;  
  52. }  
  53. }  
  54. [HttpPost]  
  55. public IActionResult Logout()  
  56. {  
  57. HttpContext.SignOutAsync(  
  58. CookieAuthenticationDefaults.AuthenticationScheme);  
  59. return RedirectToAction("Login""Login");  
  60. }  
  61. }  

Answers (2)