Token立即过期

写了一个用户退出登录的方法,让Token立即过期,也可以应用在其他场景。

1.定义ApiControllerBase

[Produces("application/json")]
[ApiController]
public abstract class ApiControllerBase : ControllerBase
{
    protected ClaimsPrincipal CurrentUser => this.HttpContext.User;
}

2.添加接口Controller

[ApiController]
[Route("[controller]")]
public class LoginController : ApiControllerBaer
{
    private readonly ILoginService loginService;
    public LoginController (ILoginService loginService)
    {
        this.loginService = loginService
    }

    [HttpPost("Logout")]
    public void Logout()
    {
        this.loginService.Logout(this.CurrentUser);
    }
}

3.添加业务逻辑层服务接口

public interface ILoginService
{
    void Logout(ClaimsPrincipal currentUser);
}

4.添加业务逻辑层实现类

public class LoginService : ILoginService
{
    private readonly ICaching cache;
    public LoginService (ICaching cache)
    {
        this.cache = cache;
    }

    public void Logout (ClaimsPrincipal currentUser)
    {
        var jwtId= currentUser.GetJwtId();
        var expiration = currentUser.GetJwtExpiration();
        var expire = DateTimeOffset.FromUnixTimeSeconds(expiration).AddMinutes(5);//jwt默认有5分钟宽限时间
        SetExpiration(jwtId,expire);
    }
    
    private const string CACHE_TOKEN_BLASKLIST_KEY = "CACHE:TOKEN:BLACKLIST";
    public void SetExpiration(string jwtId,DateTimeOffset expire)
    {
        var key = $"{CACHE_TOKEN_BLASKLIST_KEY } : {jwtId}";
        this.cache.SetString(key,jwtId, new DistributedCacheEntryOptions
        {
            AbsoluteExpiration = expire
        });
    }
}

5.添加静态类扩展方法

public static class Extensions
{
    public static string GetJwtId(this ClaimsPrincipal parincipal)
    {
        var res = parincipal.Claims.SingleOrDefault(p=> p.Type == JwtClaimTupes.JwtId);//添加JWT校验时new一个guid保存到Claim的JwtId中,例如:new Claim(JwtClaimTypes.JwtId, Guid.NewGuid().ToString())
        if(res == null)
            thew new Exception("无法获取用户JwtId。");
        return res.Value;
    }
    
    public static long GetJwtExpiration(this ClaimsPrincipal parincipal)
    {
        var res = parincipal.Claims.SingleOrDefault(p=> p.Type ==JwtClaimTupes.Expiration);
        if(res == null)
            thew new Exception("无法获取用户Expiration。");
        return Convert.ToInt64(res.Value);
    }
    
}

6.添加缓存扩展方法服务接口

public interface ICaching
{
    void SetString(string key,string value, DistributedCacheEntryOptions options);
}

7.添加缓存扩展方法服务实现类

public class Caching : ICaching
{
    private readonly IDistributedCache chche;
    public Caching(IDistributedCache chche)
    {
        this.cache = cache;
    }

    public void SetString(string key, string value, DistributedCacheEntryOptions options)
    {
        cache.SetString(key, value, options);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值