Brock has already mentioned about it, It should be at the time of login and logout
有道理,为什么它不在 Idm 中。但至少在即将到来的版本中,它绝对有可能将此作为增强功能提供。
Profile Service,IsActive 方法是被授权和命中的方法之一
令牌验证端点。
所以在登录时保持会话,然后当上面的代码命中时根据业务需求进行检查。
只要会话处于活动状态(cookie 生命周期),静默身份验证将与应用程序逻辑一起通过。所以这也可以通过 cookie 生命周期来控制。
public override async Task IsActiveAsync(IsActiveContext context)
{
var sub = context.Subject.GetSubjectId();
var user = await userManager.FindByIdAsync(sub);
//Check existing sessions
if (context.Caller.Equals("AccessTokenValidation", StringComparison.OrdinalIgnoreCase))
{
if (user != null)
context.IsActive = !appuser.VerifyRenewToken(sub, context.Client.ClientId);
else
context.IsActive = false;
}
else
context.IsActive = user != null;
}
登录
public async Task<IActionResult> Login(LoginInputModel model)
{
if (ModelState.IsValid)
{
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberLogin, false);
if (result.Succeeded)
{
//Update security stamp to invalidate existing sessions
//TODO: This didn't invalidate the existing cookie from another client
//var test= _userManager.UpdateSecurityStampAsync(_userManager.FindByEmailAsync(model.Email).Result).Result;
appUser.PersistSession(new UserSession
{
CreatedOn = DateTimeOffset.Now,
DeviceUniqueId = GetDeviceId(),
UserId = _userManager.FindByNameAsync(model.Email).Result.Id,
SId = httpContext.HttpContext.Session.Id,
ClientId= httpContext.HttpContext.Request.QueryString.Value.GetClientIdFromQuery(),
ExpiresOn = DateTimeOffset.Now.AddMinutes(appSettings.SessionTimeOut)
});
_logger.LogInformation(1, "User logged in.");
return RedirectToLocal(model.ReturnUrl);
}
当 IIS 重新启动并且用户没有正确退出时,此方法有一些缺点。
可能有更好的选择,但这不是最合适的。!
更新:
refer here duplicate/similar question
idmsrv endpoints are missing security change check
Issue raised
Should be like this @tibold