Identity LoginView model example
@model OdeToFood.ViewModels.LogInViewModel
@{
ViewBag.Title = "Login";
}
<h2>Login</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="UserName"></label>
<input asp-for="UserName" />
<span asp-validation-for="UserName"></span>
</div>
<div>
<label asp-for="Password"></label>
<input asp-for="Password" />
<span asp-validation-for="Password"></span>
</div>
<div>
<label asp-for="RememberMe"></label>
<input asp-for="RememberMe" />
<span asp-validation-for="RememberMe"></span>
</div>
<input type="submit" value="Login" />
</form>
using System.ComponentModel.DataAnnotations;
namespace OdeToFood.ViewModels
{
public class LogInViewModel
{
public string UserName { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name ="Remember Me")]
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
}
}
[HttpGet]
public IActionResult Login(string returnUrl="")
{
var model = new LogInViewModel();
model.ReturnUrl = returnUrl;
return View(model);
}
[HttpPost]
public async Task<IActionResult> Login(LogInViewModel model)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, false);
if (result.Succeeded)
{
if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
{
return Redirect(model.ReturnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
ModelState.AddModelError("", "Invalid login attempt");
return View(model);
}