Session WebAPI中的绝对和空闲会话超时
我正在使用OAuth承载身份验证创建一个WebAPI,如下所示:Session WebAPI中的绝对和空闲会话超时,session,asp.net-web-api,timeout,token,owin,Session,Asp.net Web Api,Timeout,Token,Owin,我正在使用OAuth承载身份验证创建一个WebAPI,如下所示: var oAuthServerOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/token"), AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(100), Pr
var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(100),
Provider = new AuthorizationServerProvider(),
RefreshTokenProvider = new RefreshTokenProvider(),
};
应用程序将为经过身份验证的用户生成令牌,令牌将在100分钟后过期。用户必须使用刷新令牌才能继续访问应用程序。
现在,我想更改策略如下:
var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(100),
Provider = new AuthorizationServerProvider(),
RefreshTokenProvider = new RefreshTokenProvider(),
};
- 如果用户空闲100分钟,则用户必须再次登录(应用程序必须返回401)-空闲超时
- 事件如果用户未空闲,则用户必须在8小时后再次登录-绝对超时
非常感谢。在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);
}
...
}
此时,客户端应用程序将尝试通过“刷新令牌”请求获取新的访问令牌。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 */
...
}
...
}
或者在authorizationServerProvider.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协议无关的非常特殊的解决方案
我希望它能对您有所帮助。您的解决方案听起来很不错。我会检查我当前的源代码,并尽快回复。非常感谢。:)你的回答给了我一个想法来完成我的问题。Tks。