Добавил форму входа (Login)

parent 2040f456
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using webinar65.Models;
using webinar65.ViewModels;
namespace webinar65.Controllers
{
public class AccountController : Controller
{
private UserManager<User> _userManager;
private SignInManager<User> _signInManager;
public AccountController(UserManager<User> userManager, SignInManager<User> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}
[HttpGet]
public IActionResult Login(string returnUrl)
{
ViewBag.returnUrl = returnUrl;
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(model.Email);
var result = await _signInManager.PasswordSignInAsync(
user.UserName,
model.Password,
false,
false
);
if (result.Succeeded)
return Redirect(returnUrl ?? "/");
ModelState.AddModelError(nameof(LoginViewModel.Email), "Invalid email or password");
}
return View(model);
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using webinar65.Models;
namespace webinar65.Controllers
{
[Authorize]
public class HomeController : Controller
{
public IActionResult Index()
......
using System.ComponentModel.DataAnnotations;
namespace webinar65.ViewModels
{
public class LoginViewModel
{
[Required]
[UIHint("email")]
public string Email { get; set; }
[Required]
[UIHint("password")]
public string Password { get; set; }
}
}
@model webinar65.ViewModels.LoginViewModel
@{
ViewBag.Title = "Login";
Layout = "_Layout";
}
<h2>Login</h2>
<div asp-validation-summary="All" class="text-danger"></div>
<form asp-action="Login" method="post">
<input type="hidden" name="returnUrl" value="@ViewBag.returnUrl"/>
<div class="form-group">
<label asp-for="Email">Email</label>
<input asp-for="Email" class="form-control"/>
</div>
<div class="form-group">
<label asp-for="Password">Password</label>
<input asp-for="Password" class="form-control"/>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</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