Commit 47626667 authored by Yevgeniy Agrafenin's avatar Yevgeniy Agrafenin

#2 Добавил AccountController.cs и представления входа и регистрации Login.cshtml, Register.cshtml.

parent e5ee2aee
using CafeCritic.Models;
using CafeCritic.ViewModels;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace CafeCritic.Controllers;
public class AccountController : Controller
{
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
public AccountController(UserManager<User> userManager, SignInManager<User> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
// GET
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new User
{
Email = model.Email
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, false);
return RedirectToAction("Index", "Home");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
return View("Register", model);
}
[HttpGet]
public IActionResult Login(string returnUrl = null)
{
return View(new LoginViewModel { ReturnUrl = returnUrl });
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var userName = model.Email;
if (userName != null && userName.IndexOf('@') > -1)
{
User user = await _userManager.FindByEmailAsync(model.Email);
if (user == null)
{
ModelState.AddModelError(string.Empty, "Войти не удалось");
return View(model);
}
userName = user.UserName;
}
var result = await _signInManager.PasswordSignInAsync(
userName,
model.Password,
model.RememberMe,
false);
if (result.Succeeded)
{
if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
{
return Redirect(model.ReturnUrl);
}
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError(string.Empty, "Неправильный логин и (или) пароль");
}
return View(model);
}
[HttpPost]
public async Task<IActionResult> LogOut()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Home");
}
}
\ No newline at end of file
@model CafeCritic.ViewModels.LoginViewModel
@{
ViewBag.Title = "Вход";
Layout = "_Layout";
}
<div class="row">
<div class="col-12">
<div class="card w-50">
<div class="card-body">
<form asp-action="Login" method="post" asp-antiforgery="true">
<div class="validation" asp-validation-summary="All"></div>
<div class="mb-3">
<input asp-for="Email" class="form-control" placeholder="Введите Email">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="mb-3">
<input asp-for="Password" class="form-control" placeholder="Пароль">
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<label asp-for="RememberMe" class="form-label"></label>
<input asp-for="RememberMe" type="checkbox">
<input asp-for="ReturnUrl" type="hidden"><br/>
<button class="btn btn-primary" type="submit">Вход</button>
<p class="card-body">
<a asp-action="Register">Регистрация!</a>
</p>
</form>
</div>
</div>
</div>
</div>
@section Scripts
{
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
@model dynamic
@{
ViewBag.Title = "Регистрация";
Layout = "_Layout";
}
<div class="row">
<div class="col-12">
<div class="card w-50">
<div class="card-body">
<form asp-action="Register" method="post" asp-antiforgery="true">
<div class="validation" asp-validation-summary="All"></div>
<div class="mb-3">
<input asp-for="Email" class="form-control" placeholder="Email">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="mb-3">
<input asp-for="Password" class="form-control" placeholder="Пароль">
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="mb-3">
<input asp-for="ConfirmPassword" class="form-control" placeholder="Повторите пароль">
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<button class="btn btn-primary" type="submit">Зарегистрироваться</button>
<p class="card-body">
<a asp-action="Login">Войти</a>
</p>
</form>
</div>
</div>
</div>
</div>
@section Scripts
{
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment