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

parent ae7d38b9
...@@ -19,43 +19,44 @@ public class AccountsController : Controller ...@@ -19,43 +19,44 @@ public class AccountsController : Controller
} }
[HttpGet] [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] return View(model);
// [ValidateAntiForgeryToken] }
// public async Task<IActionResult> Login(LoginViewModel model) catch (Exception e)
// { {
// try ModelState.AddModelError("", e.Message);
// { return View(model);
// 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();
// }
// }
[HttpGet] [HttpGet]
public IActionResult Register() public IActionResult Register()
...@@ -108,7 +109,7 @@ public class AccountsController : Controller ...@@ -108,7 +109,7 @@ public class AccountsController : Controller
public async Task<IActionResult> Logout() public async Task<IActionResult> Logout()
{ {
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await _signInManager.SignOutAsync();
return RedirectToAction("Login"); return RedirectToAction("Login");
} }
} }
\ No newline at end of file
...@@ -7,6 +7,9 @@ public class LoginViewModel ...@@ -7,6 +7,9 @@ public class LoginViewModel
[Required(ErrorMessage = "Введите email")] [Required(ErrorMessage = "Введите email")]
public string Email { get; set; } public string Email { get; set; }
[Required(ErrorMessage = "Введите пароль")] [Required(ErrorMessage = "Введите пароль")]
public string Password { get; set; } 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 @model Store.ViewModels.Auths.LoginViewModel
@{ <h2>Вход в приложение</h2>
ViewBag.Title = "Вход"; <form method="post" asp-controller="Accounts" asp-action="Login"
} asp-route-returnUrl="@Model.ReturnUrl">
<div asp-validation-summary="ModelOnly"></div>
<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>
<div> <div>
<div class="form-group"> <label asp-for="Email"></label><br />
<label asp-for="Email">Введите Email</label> <input asp-for="Email" />
<input type="text" asp-for="Email" />
<span asp-validation-for="Email"></span> <span asp-validation-for="Email"></span>
</div> </div>
<div class="form-group"> <div>
<label asp-for="Password">Введите пароль</label> <label asp-for="Password"></label><br />
<input asp-for="Password" /> <input asp-for="Password" />
<span asp-validation-for="Password"></span> <span asp-validation-for="Password"></span>
</div> </div>
<div class="form-group"> <div>
<input type="submit" value="Войти" class="btn" /> <label asp-for="RememberMe"></label><br />
<input asp-for="RememberMe" />
</div> </div>
<div>
<input type="submit" value="Войти" />
</div> </div>
</form> </form>
\ No newline at end of file
...@@ -25,23 +25,33 @@ ...@@ -25,23 +25,33 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a> <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li> </li>
</ul>
</div>
<div class="login_group">
@if (User.Identity.IsAuthenticated) @if (User.Identity.IsAuthenticated)
{ {
<li class="nav-item"> <p>@User.Identity.Name</p>
<a class="nav-link text-dark" asp-area="" asp-controller="Accounts" asp-action="Logout">Выход</a>
</li> <form method="post" asp-controller="Accounts" asp-action="Logout">
<input class="nav-link text-dark" type="submit" value="Выход"/>
</form>
} }
else else
{ {
<ul class="navbar-nav flex-grow-1">
<li class="nav-item"> <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>
<li class="nav-item"> <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> </li>
}
</ul> </ul>
}
</div> </div>
</div> </div>
</nav> </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