示例
1、AccountController.cs中添加SignInManager
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<ApplicationRole> _roleManager;
private readonly SignInManager<ApplicationUser> _signInManager;
public AccountController(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager, SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_roleManager = roleManager;
_signInManager = signInManager;
}
2、result.Succeeded中添加SignInAsync
if (result.Succeeded)
{
//Sign in
await _signInManager.SignInAsync(applicationUser, false);
return RedirectToAction(nameof(PersonsController.Index), "Persons");
}
3、显示当前用户到页面右上角
_Layout.cshtml中添加@User.Identity?.Name
<ul>
<li>
@User.Identity?.Name
</li>
<li>
<a asp-controller="Account" asp-action="Register">Register</a>
<a asp-controller="Account" asp-action="Login">Login</a>
</li>
</ul>
4、RegisterDTO.cs中验证两遍密码是否相同
[Required(ErrorMessage = "Confirm Password can't be blank")]
[DataType(DataType.Password)]
[Compare("Password",ErrorMessage = "Password and confirm password do not match")]
public string ConfirmPassword { get; set; }
运行程序后,注册完可以直接登录成功。
Gitee获取源码:
https://gitee.com/huang_jianhua0101/asp.-net-core-8.git
标签:SignInManager,userManager,roleManager,SignIn,Manager,private,Password,285,signIn From: https://blog.csdn.net/KevinHuang2088/article/details/139688359