Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Hotel.Controllers
- {
- public class AccountController : Controller
- {
- private UserContext db;
- public AccountController(UserContext context)
- {
- db = context;
- }
- [HttpGet]
- public IActionResult Login()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> Login(LoginModel model)
- {
- if (ModelState.IsValid)
- {
- User user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email && u.Password == model.Password);
- if (user != null)
- {
- await Authenticate(model.Email); // аутентификация
- return RedirectToAction("Index", "Home");
- }
- ModelState.AddModelError("", "Некорректные логин и(или) пароль");
- }
- return View(model);
- }
- [HttpGet]
- public IActionResult Register()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> Register(RegisterModel model)
- {
- if (ModelState.IsValid)
- {
- User user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email);
- if (user == null)
- {
- // добавляем пользователя в бд
- db.Users.Add(new User { Email = model.Email, Password = model.Password });
- await db.SaveChangesAsync();
- // await Authenticate(model.Email); // аутентификация
- return RedirectToAction("Index", "Home");
- }
- else
- ModelState.AddModelError("", "Некорректные логин и(или) пароль");
- }
- return View(model);
- }
- private async Task Authenticate(string userName)
- {
- // создаем один claim
- var claims = new List<Claim>
- {
- new Claim(ClaimsIdentity.DefaultNameClaimType, userName)
- };
- // создаем объект ClaimsIdentity
- ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
- ClaimsIdentity.DefaultRoleClaimType);
- // установка аутентификационных куки
- await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(id));
- }
- public async Task<IActionResult> Logout()
- {
- await HttpContext.Authentication.SignOutAsync("Cookies");
- return RedirectToAction("Login", "Account");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement