Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. namespace Hotel.Controllers
  2. {
  3.     public class AccountController : Controller
  4.     {
  5.         private UserContext db;
  6.         public AccountController(UserContext context)
  7.         {
  8.             db = context;
  9.         }
  10.         [HttpGet]
  11.         public IActionResult Login()
  12.         {
  13.             return View();
  14.         }
  15.         [HttpPost]
  16.         [ValidateAntiForgeryToken]
  17.         public async Task<IActionResult> Login(LoginModel model)
  18.         {
  19.             if (ModelState.IsValid)
  20.             {
  21.                 User user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email && u.Password == model.Password);
  22.                 if (user != null)
  23.                 {
  24.                     await Authenticate(model.Email); // аутентификация
  25.  
  26.                     return RedirectToAction("Index", "Home");
  27.                 }
  28.                 ModelState.AddModelError("", "Некорректные логин и(или) пароль");
  29.             }
  30.             return View(model);
  31.         }
  32.         [HttpGet]
  33.         public IActionResult Register()
  34.         {
  35.             return View();
  36.         }
  37.         [HttpPost]
  38.         [ValidateAntiForgeryToken]
  39.         public async Task<IActionResult> Register(RegisterModel model)
  40.         {
  41.             if (ModelState.IsValid)
  42.             {
  43.                 User user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email);
  44.                 if (user == null)
  45.                 {
  46.                     // добавляем пользователя в бд
  47.                     db.Users.Add(new User { Email = model.Email, Password = model.Password });
  48.                     await db.SaveChangesAsync();
  49.  
  50.                    // await Authenticate(model.Email); // аутентификация
  51.  
  52.                     return RedirectToAction("Index", "Home");
  53.                 }
  54.                 else
  55.                     ModelState.AddModelError("", "Некорректные логин и(или) пароль");
  56.             }
  57.             return View(model);
  58.         }
  59.  
  60.         private async Task Authenticate(string userName)
  61.         {
  62.             // создаем один claim
  63.             var claims = new List<Claim>
  64.             {
  65.                 new Claim(ClaimsIdentity.DefaultNameClaimType, userName)
  66.             };
  67.             // создаем объект ClaimsIdentity
  68.             ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
  69.                 ClaimsIdentity.DefaultRoleClaimType);
  70.             // установка аутентификационных куки
  71.             await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(id));
  72.         }
  73.  
  74.         public async Task<IActionResult> Logout()
  75.         {
  76.             await HttpContext.Authentication.SignOutAsync("Cookies");
  77.             return RedirectToAction("Login", "Account");
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement