Изменил дефолтную авторизацию на IdentityCore

parent ae7d38b9
......@@ -19,43 +19,44 @@ public class AccountsController : Controller
}
[HttpGet]
public IActionResult Login()
public IActionResult Login(string? returnUrl = null)
{
return View();
return View(new LoginViewModel{ReturnUrl = returnUrl!});
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
try
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(model.Email);
var result = await _signInManager.PasswordSignInAsync(
user, model.Password, model.RememberMe, false);
if (result.Succeeded)
{
if (!string.IsNullOrWhiteSpace(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
return Redirect(model.ReturnUrl);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError(string.Empty, "Неверный логин или/и пароль");
return View(model);
}
// [HttpPost]
// [ValidateAntiForgeryToken]
// public async Task<IActionResult> Login(LoginViewModel model)
// {
// try
// {
// if (ModelState.IsValid)
// {
// var user = _userRepository.GetUserByEmail(model.Email);
// if (user is not null)
// {
// if (user.Password.Equals(model.Password))
// {
// await AuthenticateAsync(user);
// return RedirectToAction("Index", "Home");
// }
// ModelState.AddModelError("", "пароль введен неверно");
// }
// else
// {
// ModelState.AddModelError("", "пользователь не найден");
// }
// }
//
// return View(model);
// }
// catch (Exception e)
// {
// Console.WriteLine(e);
// return View();
// }
// }
return View(model);
}
catch (Exception e)
{
ModelState.AddModelError("", e.Message);
return View(model);
}
}
[HttpGet]
public IActionResult Register()
......@@ -108,7 +109,7 @@ public class AccountsController : Controller
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await _signInManager.SignOutAsync();
return RedirectToAction("Login");
}
}
\ No newline at end of file
......@@ -7,6 +7,9 @@ public class LoginViewModel
[Required(ErrorMessage = "Введите email")]
public string Email { get; set; }
[Required(ErrorMessage = "Введите пароль")]
public string Password { get; set; }
[Display(Name = "Запомнить?")]
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
}
\ No newline at end of file
@using Microsoft.AspNetCore.Mvc.TagHelpers
@model Store.ViewModels.Auths.LoginViewModel
@{
ViewBag.Title = "Вход";
}
<h2>Вход на сайт</h2>
<a asp-action="Register" asp-controller="Accounts">Регистрация</a>
<form asp-action="Login" asp-controller="Accounts" asp-anti-forgery="true">
<div class="validation" asp-validation-summary="ModelOnly"></div>
<h2>Вход в приложение</h2>
<form method="post" asp-controller="Accounts" asp-action="Login"
asp-route-returnUrl="@Model.ReturnUrl">
<div asp-validation-summary="ModelOnly"></div>
<div>
<div class="form-group">
<label asp-for="Email">Введите Email</label>
<input type="text" asp-for="Email" />
<label asp-for="Email"></label><br />
<input asp-for="Email" />
<span asp-validation-for="Email"></span>
</div>
<div class="form-group">
<label asp-for="Password">Введите пароль</label>
<div>
<label asp-for="Password"></label><br />
<input asp-for="Password" />
<span asp-validation-for="Password"></span>
</div>
<div class="form-group">
<input type="submit" value="Войти" class="btn" />
<div>
<label asp-for="RememberMe"></label><br />
<input asp-for="RememberMe" />
</div>
<div>
<input type="submit" value="Войти" />
</div>
</form>
\ No newline at end of file
......@@ -25,23 +25,33 @@
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
<div class="login_group">
@if (User.Identity.IsAuthenticated)
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Accounts" asp-action="Logout">Выход</a>
</li>
<p>@User.Identity.Name</p>
<form method="post" asp-controller="Accounts" asp-action="Logout">
<input class="nav-link text-dark" type="submit" value="Выход"/>
</form>
}
else
{
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Accounts" asp-action="Login">Вход</a>
<a asp-controller="Accounts" asp-action="Login" class="nav-link text-dark">Вход</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Accounts" asp-action="Register">Регистрация</a>
<a asp-controller="Accounts" asp-action="Register" class="nav-link text-dark">Регистрация</a>
</li>
}
</ul>
}
</div>
</div>
</nav>
......
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