c# - .Net5 身份注销

标签 c# .net jwt asp.net-identity

如何注销使用 .Net 5 Identity 系统登录的用户?

当我调用 Logout 时,jwt token 保持有效,我可以成功调用授权请求。

 await _signInManager.SignOutAsync();

这是我的用户 Controller :

[ApiController]
[Route("api/[controller]/[action]")]
[Authorize(AuthenticationSchemes = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)]
public class UserController : ControllerBase
{
    private readonly UserManager<IdentityUser> _userManager;
    private readonly RoleManager<IdentityRole> _roleManager;
    private readonly SignInManager<IdentityUser> _signInManager;
    private readonly IConfiguration _configuration;
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly ILogger<UserController> _logger;
    
    public UserController(IConfiguration configuration, SignInManager<IdentityUser> signInManager,
        UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager,
        IHttpContextAccessor httpContextAccessor, ILogger<UserController> logger)
    {
        _configuration = configuration;
        _logger = logger;
        _signInManager = signInManager;
        _userManager = userManager;
        _roleManager = roleManager;
        _httpContextAccessor = httpContextAccessor;
    }

    public async Task<IActionResult> Logout() { 
        await _signInManager.SignOutAsync();
        return Ok("Logged out.");
    } 
}

我使用的是5.0.100 sdk版本。

谢谢!

最佳答案

一般来说,不可能撤销 JWT 不记名 token ,这就是它们有过期时间的原因。这个想法是,用户代理在时间到期后返回并刷新 token ,此时它会知道 session 是否已结束。

JWT token 的属性之一是可以“离线”验证,即无需访问发行后端。所需要的只是用于签名的 key 的公共(public)部分。 token 发出后,它在 token 内设置的持续时间内有效,并且没有(规范的)方法使其过期。

当然,您始终可以在后端自由构建撤销列表或类似内容,但这实际上不是 JWT token 的用途。

缓解问题的一个简单方法是设置较短的过期时间。

关于c# - .Net5 身份注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65673640/

相关文章:

c# - 编程语言,您可以在其中通过 引用数组元素。 (点)

c# - 即时格式化 html 日期和数字

c# - 如何在事件处理程序内调用 WebBrowser 上的方法?

node.js - res.cookie未在浏览器中设置cookie

c# - 在objectlistview treelistview中查找parent

c# - 使用 Moq.It.IsAny 测试以某物开头的字符串

c# - 如何联合两个数据表并对结果进行排序

c# - 有时,缩小位图会生成更大的文件。为什么?

oauth - JWT 和 Bearer Token 有什么区别?

web-services - 防止 token 共享的最佳方法是什么?