c# - C# OWIN Security - 设置过期令牌 - 始终具有默认值
问题描述
大家好,我目前有一个使用 owin security 的项目
当我尝试向 / 令牌发出请求时,我得到了这个
那里指定到期令牌为 7199 秒(2 小时)
我正在寻找这个端点(路线),但我不喜欢 /token它或找到他们将此值设置为 2 小时的地方(查看整个解决方案)
我发现的唯一一件事是这个类对应于刷新令牌(但没有过期令牌),但是这个令牌设置为 14400,但是当我再次发出请求时,令牌总是保持在那个值
namespace Conarch.Providers
{
public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
{
public async Task CreateAsync(AuthenticationTokenCreateContext context)
{
var clientid = context.Ticket.Properties.Dictionary["as:client_id"];
context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(Convert.ToDouble(12000));
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);
}
}
}
public void Create(AuthenticationTokenCreateContext context)
{
throw new NotImplementedException();
}
public void Receive(AuthenticationTokenReceiveContext context)
{
throw new NotImplementedException();
}
我的问题是:你在什么地方设置这个值,时间怎么会增加?
非常感谢你
解决方案
您必须在 Web 应用程序配置期间设置过期时间使用此:
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = new AuthorizationServerProvider(),
RefreshTokenProvider = new RefreshTokenProvider()
};
你可以在这里找到完整的文章
推荐阅读
- solidity - GameFi中价格滑点的处理策略
- google-maps - Ionic Cordova GoogleMpas Markers Cluster 缩小时不显示分组
- unity3d - 所以我试图在我的游戏中制作死亡/重生的东西我正在使用角色对撞机但它不起作用请帮助
- c# - 简而言之,在 C# 中,我无法理解这部分
- ssis - SSIS 类型 2 SCD
- python - 拆分和操作 Pandas DataFrame 以进行多次绘图
- python - “查找访问代码”Google Foobar 挑战
- ruby-on-rails - 在 Windows 服务器上使用带有 Rails 5.2 的 Nokogiri 时出现无效参数 @ rb_sysopen 错误
- arrays - 同时使用两个进程(父进程和子进程)的分段错误
- php - 尝试在 Laravel 单元测试中第一次运行测试用例时出现 CommandNotFoundException