我看不到在 OAuth 2.0 中同时出现两个超时的方法。
仅关于第一次超时,空闲超时,可以设置刷新令牌超时为100分钟。访问令牌超时时间会更短,每次访问令牌过期时,您都会获得新的访问令牌和刷新令牌。如果用户会话空闲超过 100 分钟,当应用尝试刷新令牌时,oauth 服务器会意识到刷新令牌已过期且无效。然后用户需要输入他们的凭据。
对于第二次超时,您可以将访问令牌超时设置为 8 小时,并且不实施刷新令牌。
考虑到令牌将被发送到资源服务器,这不能与 oauth 服务器相同。资源服务器只能检查令牌中的票证是否未过期,但无法控制用户输入凭据后第一次授予令牌的时间。
如果您同时控制 oauth 和资源服务器,则可以采取一种变通方法,为刷新令牌实现 100 分钟超时,并在票证中包含用户输入凭据的时间属性。请以以下代码为例:
public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
...
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
...
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"client_id", clientId
},
{
"ownerCredentialsTimestamp", DateTime.UtcNow.ToString()
}
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
}
...
}
当资源服务器获取到token中包含的ticket时,可以将属性中的值与当前时间进行比较。在差异大于 8 小时的情况下可以返回 401 - Unauthorized 响应,强制客户端应用请求另一个访问令牌:
public class AccessTokenProvider : IAuthenticationTokenProvider
{
public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
{
context.DeserializeTicket(context.Token);
if (context.Ticket.Properties.Dictionary["ownerCredentialsTimestamp"] != null)
{
var ownerCredentialsTimestamp = Convert.ToDateTime(context.Ticket.Properties.Dictionary["ownerCredentialsTimestamp"]).ToUniversalTime();
if (/* difference is bigger than 8 hours */)
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
}
}
}
}
此时,客户端应用程序将尝试通过“refresh_token”请求获取新的访问令牌。 oauth 服务器必须再次检查与当前刷新令牌相关的最后输入凭据的时间,数据库表中可能有一列存储刷新令牌(如果这是您的情况)。
您可以在RefreshTokenProvider.ReceiveAsync() 方法中查看:
public class RefreshTokenProvider : IAuthenticationTokenProvider
{
...
public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
{
...
/* Check the received refresh token, including the last time that the credentials were entered for this user */
...
}
...
}
或者在AuthorizationServerProvicer.GrantRefreshToken()方法中:
public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
...
public override async Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
...
/* Check the last time that the credentials were entered for this user */
...
}
...
}
这是一个非常特殊的解决方案,与 OAuth 2.0 协议无关。
希望对你有帮助。