首先:
您将如何确保会话被“销毁”?
你不能确定。用户可能会失去与互联网的连接或硬关机。
现在,回答您的问题.. 有点:
您需要的是一个过期的令牌。这将确保用户在一段时间后退出。你可以通过设置一个相对较短的过期时间来做到这一点,然后确保每当用户使用密钥访问 Web 服务时我都会重置这个时间。
Taiseer 的 Bit of Tech 教程非常棒,有助于了解身份框架和基于令牌的身份验证的基础知识。
Taiseer 在 Startup.cs 中配置 OAuth 时会在其访问令牌上设置过期时间:
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), //<---- Right here
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
你可以这样做:
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30)
然后您必须找到一种方法来在每次使用令牌时刷新令牌的生命周期。我目前没有给你的建议,但也许其他人有。您可以通过覆盖 AuthorizeAttribute 来做到这一点。
Taiseer 也有关于 RefreshTokens 的精彩指南,可能会派上用场:
http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/