Advertisement
Dr4noel

Pt Joni

Jul 9th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public async Task<IActionResult> Login(UserData credentials)
  4. {
  5. if (credentials.Password == null || credentials.Username == null)
  6. {
  7. ViewBag.LoginFailed = "Something went wrong. Please try again.";
  8. return View("LoginForm");
  9. }
  10. else
  11. if (DBClases.Login.VerifyLogin(credentials))
  12. {
  13. #region Claims
  14. List<Claim> claims = new List<Claim>
  15. {
  16. new Claim(ClaimTypes.Name, credentials.Username),
  17. new Claim(ClaimTypes.SerialNumber, credentials.Id.ToString())
  18. };
  19.  
  20. ClaimsIdentity identity = new ClaimsIdentity(claims, "cookie");
  21.  
  22. ClaimsPrincipal principal = new ClaimsPrincipal(identity);
  23.  
  24. await HttpContext.SignInAsync(
  25. scheme: "SnippetManager",
  26. principal: principal,
  27. properties: new AuthenticationProperties
  28. {
  29.  
  30. });
  31. #endregion
  32. return base.RedirectToAction("Index", "Home");
  33. }
  34. else
  35. {
  36. return base.View("LoginForm");
  37. }
  38. }
  39. //#####Ce e mai sus am in "AccountControler" la functia de login#####//
  40.  
  41. public void ConfigureServices(IServiceCollection services)
  42. {
  43. services.AddAuthentication("SnippetManager")
  44. .AddCookie("SnippetManager", options =>
  45. {
  46. options.Cookie = new CookieBuilder
  47. {
  48. HttpOnly = true,
  49. Name = "Snippet.Manager.Cookie",
  50. Path = "/",
  51. SameSite = SameSiteMode.Strict,
  52. SecurePolicy = CookieSecurePolicy.SameAsRequest
  53. };
  54. options.SlidingExpiration = true;
  55. options.LoginPath = new PathString("/Account/Login");
  56. options.LogoutPath = new PathString("/Account/Login");
  57. options.ReturnUrlParameter = "ReturnUrl";
  58. });
  59.  
  60. services.AddMvc(/*options => {options.Filters.Add<CustomAuthentication>();}*/);
  61. }
  62.  
  63. //#####Ce e mai sus am in Starup.cs####//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement