Изменил дефолтную авторизацию на 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] [HttpPost]
// [ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
// public async Task<IActionResult> Login(LoginViewModel model) public async Task<IActionResult> Login(LoginViewModel model)
// { {
// try try
// { {
// if (ModelState.IsValid) if (ModelState.IsValid)
// { {
// var user = _userRepository.GetUserByEmail(model.Email); var user = await _userManager.FindByEmailAsync(model.Email);
// if (user is not null) var result = await _signInManager.PasswordSignInAsync(
// { user, model.Password, model.RememberMe, false);
// if (user.Password.Equals(model.Password))
// { if (result.Succeeded)
// await AuthenticateAsync(user); {
// return RedirectToAction("Index", "Home"); if (!string.IsNullOrWhiteSpace(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
// } return Redirect(model.ReturnUrl);
// ModelState.AddModelError("", "пароль введен неверно");
// } return RedirectToAction("Index", "Home");
// else }
// {
// ModelState.AddModelError("", "пользователь не найден"); ModelState.AddModelError(string.Empty, "Неверный логин или/и пароль");
// }
// } return View(model);
// }
// return View(model);
// } return View(model);
// catch (Exception e) }
// { catch (Exception e)
// Console.WriteLine(e); {
// return View(); ModelState.AddModelError("", e.Message);
// } return View(model);
// } }
}
[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> <div>
<label asp-for="Email"></label><br />
<a asp-action="Register" asp-controller="Accounts">Регистрация</a> <input asp-for="Email" />
<span asp-validation-for="Email"></span>
<form asp-action="Login" asp-controller="Accounts" asp-anti-forgery="true"> </div>
<div class="validation" asp-validation-summary="ModelOnly"></div> <div>
<div> <label asp-for="Password"></label><br />
<div class="form-group"> <input asp-for="Password" />
<label asp-for="Email">Введите Email</label> <span asp-validation-for="Password"></span>
<input type="text" asp-for="Email" /> </div>
<span asp-validation-for="Email"></span> <div>
</div> <label asp-for="RememberMe"></label><br />
<div class="form-group"> <input asp-for="RememberMe" />
<label asp-for="Password">Введите пароль</label> </div>
<input asp-for="Password" /> <div>
<span asp-validation-for="Password"></span> <input type="submit" value="Войти" />
</div> </div>
<div class="form-group">
<input type="submit" value="Войти" class="btn" />
</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>
@if (User.Identity.IsAuthenticated) </ul>
{ </div>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Accounts" asp-action="Logout">Выход</a> <div class="login_group">
</li> @if (User.Identity.IsAuthenticated)
} {
else <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"> <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