Добавить авторизацию.

***
- Добавил авторизацию пользователя.
***
parent 79c5f84a
......@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PhoneStore.Enums;
using PhoneStore.Helpers;
using PhoneStore.Models;
using PhoneStore.Services.Abstractions;
using PhoneStore.ViewModels.Account;
......@@ -39,5 +40,31 @@ namespace PhoneStore.Controllers
return View(model);
}
[HttpGet]
public IActionResult Login(string returnUrl = null)
{
return View(new LoginViewModel { ReturnUrl = returnUrl });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (!ModelState.IsValid)
return View(model);
var result = await _accountService.LogIn(model);
if (result.StatusCodes == StatusCodes.Success)
{
if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
return Redirect(model.ReturnUrl);
return RedirectToAction("Index", "Phones");
}
if (result.ErrorMessages.Any())
ModelState.AddErrors(result.ErrorMessages);
return View(model);
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using PhoneStore.Enums;
......@@ -24,6 +26,12 @@ namespace PhoneStore.Services
public async Task<IdentityResult> Register(RegisterViewModel model)
{
if (model is null) return new IdentityResult
{
StatusCodes = StatusCodes.Error,
ErrorMessages = new List<string>{"Внутренняя ошибка"}
};
User user = new User
{
Email = model.Email,
......@@ -44,12 +52,25 @@ namespace PhoneStore.Services
ErrorMessages = errors,
StatusCodes = StatusCodes.Error
};
}
public Task<IdentityResult> LogIn(LoginViewModel model)
public async Task<IdentityResult> LogIn(LoginViewModel model)
{
throw new System.NotImplementedException();
User user = await _userManager.FindByEmailAsync(model.Email);
SignInResult result = await _signInManager.PasswordSignInAsync(
user,
model.Password,
model.RememberMe,
false
);
if (result.Succeeded)
return new IdentityResult {StatusCodes = StatusCodes.Success};
return new IdentityResult
{
StatusCodes = StatusCodes.Error,
ErrorMessages = new List<string>{"Неправильный логин и (или) пароль"}
};
}
public Task<IdentityResult> LogOf()
......
@model LoginViewModel
@{
ViewBag.Title = "title";
Layout = "_Layout";
}
<h2>Вход в приложение</h2>
<form method="post" asp-controller="Account" asp-action="Login"
asp-route-returnUrl="@Model.ReturnUrl">
<div asp-validation-summary="ModelOnly"></div>
<div>
<label asp-for="Email"></label><br />
<input asp-for="Email" />
<span asp-validation-for="Email"></span>
</div>
<div>
<label asp-for="Password"></label><br />
<input asp-for="Password" />
<span asp-validation-for="Password"></span>
</div>
<div>
<label asp-for="RememberMe"></label><br />
<input asp-for="RememberMe" />
</div>
<div>
<button type="submit" class="btn btn-outline-warning">Войти</button>
</div>
</form>
......@@ -33,7 +33,7 @@
<span asp-validation-for="PasswordConfirm"></span>
</div>
<div>
<input type="submit" value="Регистрация" />
<button type="submit" class="btn btn-outline-warning">Регистрация</button>
</div>
</form>
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