三、使用UserManager、SignInManager验证操作
新建一个 AccountController 控制器 并在构造函数中获取 依赖注入的对象 UserManager 与 SignInManager 如下:
[Authorize]
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger _logger;
public AccountController(UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
ILoggerFactory loggerFactory)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = loggerFactory.CreateLogger<AccountController>();
}
}
SignInManager 是提供用户登录登出的API ,UserManager 是提供用户管理的API。
接着来实现一下简单的登录登出。
/// <summary>
/// 登录
/// </summary>
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(ReqLoginModel req)
{
var json = new JsonResultModel<object>();
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(req.UserName, req.Password, isPersistent: true, lockoutOnFailure: false);
if (result.Succeeded)
{
json.code = "200";
json.message = "登录成功";
}
else
{
json.code = "400";
json.message = "登录失败";
}
if (result.IsLockedOut)
{
json.code = "401";
json.message = "账户密码已错误3次,账户被锁定,请30分钟后再尝试";
}
}
else
{
var errorMessges = ModelState.GetErrorMessage();
json.code = "403";
json.message = string.Join(",", errorMessges);
}
return json.ToJsonResult();
}
/// <summary>
/// 登出
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> LogOut()
{await _signInManager.SignOutAsync();
var json = new JsonResultModel<object>()
{
code = "200",
data = null,
message = "登出成功",
remark = string.Empty
};
return json.ToJsonResult();
}
四、使用Identity配置
在 ConfigureServices 方法中加入
services.Configure<IdentityOptions>(options =>
{
// 密码配置
options.Password.RequireDigit = false;//是否需要数字(0-9).
options.Password.RequiredLength = 6;//设置密码长度最小为6
options.Password.RequireNonAlphanumeric = false;//是否包含非字母或数字字符。
options.Password.RequireUppercase = false;//是否需要大写字母(A-Z).
options.Password.RequireLowercase = false;//是否需要小写字母(a-z).
//options.Password.RequiredUniqueChars = 6;
// 锁定设置
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);//账户锁定时长30分钟
options.Lockout.MaxFailedAccessAttempts = 3;//10次失败的尝试将账户锁定
//options.Lockout.AllowedForNewUsers = true;
// 用户设置
options.User.RequireUniqueEmail = false; //是否Email地址必须唯一
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
//options.Cookie.Expiration = TimeSpan.FromMinutes(30);//30分钟
options.Cookie.Expiration = TimeSpan.FromHours(12);//12小时
options.LoginPath = "/api/Account/NotLogin"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
//options.LogoutPath = "/api/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
//options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
options.SlidingExpiration = true;
});








