我正在使用 asp.net web api 和基于 token 的身份验证。我的 token 选项将成功 token 过期时间设置为 14 天后。
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new SimpleAuthorizationServerProvider(),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true
};
而我生成的token是这样的。
{
"access_token": "Mg5oQAzt9RLSUezxPYNQ7JRcJqo-uPxfOgOGHKkrJ_q56g0H8x-sjKm1mkjND0VAK3H22nwFlGNk5wfTocCb5mKMvTYwsZAv5sh0SACHdbb_9BGftMuqbFdm6wH0wXF-Sq3noF7hc2FeUUauhDfrBq5jTSou4NO3EBwjc0jj3v-yQzPGMCFxq2Q8C9uhat14rGYteYqj5mX3L5JtwHrobePL2R9lcnagNIMa44GUWQ8DVR2urP4KCrDHJf1G5pIfv94uB85I7sbf0nse7VvhYp058I8voNR9_zD1XC5-AODQJ16F484zxQokX1BWJ3gfgd86zJr2O9iKsA",
"token_type": "bearer",
"expires_in": 1209599,
".issued": "Sat, 20 Feb 2016 13:15:10 GMT",
".expires": "Sat, 05 Mar 2016 13:15:10 GMT"
}
我将此信息保存在 cookie 中并在我的应用程序中使用。但是几分钟后,我的访问 token 验证即将过期并且出现 401 http 错误。
我正在尝试从 Fiddler 和 postman 那里获取请求,但给出了 401 授权错误。
最佳答案
来自这篇文章:ASP.NET Web API Authorization tokens expiring early
看起来每次回收应用程序池时都会重新生成用于加密和解密 token 的机器 key ,导致我们的应用程序无法解密以前加密的 token 。尝试按照帖子中的建议设置固定的机器 key 。
旁注:
从应用程序设计的角度来看,我们不应该为访问 token 设置很大的时间跨度,访问 token 应该是短暂的并且与刷新 token 一起使用:Why Does OAuth v2 Have Both Access and Refresh Tokens?
为了在 owin 中生成刷新 token ,请将 RefreshTokenProvider
提供给您的 OAuthAuthorizationServerOptions
:
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new SimpleAuthorizationServerProvider(),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
// In production mode set AllowInsecureHttp = false
AllowInsecureHttp = true,
RefreshTokenProvider = //your refresh token provider.
};
关于c# - 几分钟后 Web api 不记名 token 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523870/