Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Security.Claims;
  3. using System.Threading.Tasks;
  4. using Ad.ViewModels;
  5. using Microsoft.AspNetCore.Authentication;
  6. using Microsoft.AspNetCore.Authentication.Cookies;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Identity;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.EntityFrameworkCore;
  11. using Site.Data;
  12. using Site.Data.Models;
  13.  
  14. namespace Ad.Controllers
  15. {
  16. [AllowAnonymous]
  17. public class AccountController : Controller
  18. {
  19. private SiteDbContext _context;
  20. public AccountController(SiteDbContext context)
  21. {
  22. _context = context;
  23. }
  24.  
  25. public IActionResult Login() => View();
  26.  
  27. [HttpPost]
  28. public async Task<IActionResult> Login(LoginViewModel model)
  29. {
  30. if (ModelState.IsValid)
  31. {
  32. User user = await _context.Users
  33. .Include(u => u.Role)
  34. .FirstOrDefaultAsync(u => u.UserName == model.UserName && u.Password == model.Password);
  35. if (user != null)
  36. {
  37. await Authenticate(user); // аутентификация
  38.  
  39. return RedirectToAction("Index", "Profile");
  40. }
  41. ModelState.AddModelError("", "Некорректный логин и(или) пароль");
  42. }
  43. return View(model);
  44. }
  45.  
  46. private async Task Authenticate(User user)
  47. {
  48. // создаем один claim
  49. var claims = new List<Claim>
  50. {
  51. new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserName),
  52. new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role?.Name)
  53. };
  54. // создаем объект ClaimsIdentity
  55. ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
  56. ClaimsIdentity.DefaultRoleClaimType);
  57. // установка аутентификационных куки
  58. await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id));
  59. }
  60.  
  61. public IActionResult Register()
  62. {
  63. return View();
  64. }
  65.  
  66. [HttpPost]
  67. [ValidateAntiForgeryToken]
  68. public async Task<IActionResult> Register(RegisterModel model)
  69. {
  70. if (ModelState.IsValid)
  71. {
  72. User user = await _context.Users.FirstOrDefaultAsync(u => u.UserName == model.UserName);
  73. if (user == null)
  74. {
  75. // добавляем пользователя в бд
  76. user = new User { UserName = model.UserName, Email = model.Email, Password = HashPassword(model.Password) };
  77. Role userRole = await _context.Roles.FirstOrDefaultAsync(r => r.Name == "User");
  78. if (userRole != null)
  79. user.Role = userRole;
  80.  
  81. _context.Users.Add(user);
  82. await _context.SaveChangesAsync();
  83.  
  84. await Authenticate(user); // аутентификация
  85.  
  86. return RedirectToAction("Login", "Account");
  87. }
  88. else
  89. ModelState.AddModelError("", "Некорректные логин и(или) пароль");
  90. }
  91. return View(model);
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement