gpt4 book ai didi

c# - 刷新 token 在访问 token 后立即过期

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:35 26 4
gpt4 key购买 nike

我正在实现 JWT 刷新 token ,并为刷新 token 设置不同的过期时间,但它的过期时间与访问 token 相同

var refreshTokenId = Guid.NewGuid().ToString("n");
DateTime refreshTokenLifeTime = context.OwinContext.Get<DateTime>("as:clientRefreshTokenLifeTime");

保存到数据库

RefreshToken refreshToken = new RefreshToken();
refreshToken.Token = refreshTokenId;
refreshToken.PrivateKey = context.SerializeTicket();
refreshToken.ExpiryDate = refreshTokenLifeTime;

结束保存Db

context.Ticket.Properties.IssuedUtc = DateTime.Now;
context.Ticket.Properties.ExpiresUtc = refreshTokenLifeTime;

context.SetToken(refreshTokenId);
context.SetToken(context.SerializeTicket());

对我做错了什么有什么帮助吗?

最佳答案

刷新 token 不会延长过期时间,这称为滑动过期,您无法使用访问 token 来做到这一点。我使用刷新 token 来更新用户角色,而不是过期时间。检查这个Link滑动过期我使用下面的代码来刷新 token 并保存它

  public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
{

public async Task CreateAsync(AuthenticationTokenCreateContext context)
{
var clientid = context.Ticket.Properties.Dictionary["as:client_id"];

if (string.IsNullOrEmpty(clientid))
{
return;
}

var refreshTokenId = Guid.NewGuid().ToString("n");

using (AuthRepository _repo = new AuthRepository())
{
var refreshTokenLifeTime = context.OwinContext.Get<string>("as:clientRefreshTokenLifeTime");

var token = new RefreshToken()
{
Id = Helper.GetHash(refreshTokenId),
ClientId = clientid,
Subject = context.Ticket.Identity.Name,
IssuedUtc = DateTime.UtcNow,
ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(refreshTokenLifeTime))
};

context.Ticket.Properties.IssuedUtc = token.IssuedUtc;
context.Ticket.Properties.ExpiresUtc = token.ExpiresUtc;

token.ProtectedTicket = context.SerializeTicket();

var result = await _repo.AddRefreshToken(token);

if (result)
{
context.SetToken(refreshTokenId);
}

}
}

public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
{

var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

string hashedTokenId = Helper.GetHash(context.Token);

using (AuthRepository _repo = new AuthRepository())
{
var refreshToken = await _repo.FindRefreshToken(hashedTokenId);

if (refreshToken != null )
{
//Get protectedTicket from refreshToken class
context.DeserializeTicket(refreshToken.ProtectedTicket);
var result = await _repo.RemoveRefreshToken(hashedTokenId);
}
}
}
}

现在请求上下文包含之前为该用户存储的所有声明,您需要添加允许您发布新声明或更新现有声明并将它们包含到之前生成的新访问 token 中的逻辑您需要在您拥有的 AuthorizationServerProvider 类中添加以下代码。

public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
var currentClient = context.ClientId;

if (originalClient != currentClient)
{
context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
return Task.FromResult<object>(null);
}

// Change auth ticket for refresh token requests
var newIdentity = new ClaimsIdentity(context.Ticket.Identity);
newIdentity.AddClaim(new Claim("newClaim", "newValue"));

var newTicket = new AuthenticationTicket(newIdentity, context.Ticket.Properties);
context.Validated(newTicket);

return Task.FromResult<object>(null);
}

关于c# - 刷新 token 在访问 token 后立即过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com