我基于Taiseer's tutorial创建了一个JWT令牌实现。
以下代码已添加到我的Owin启动类中:
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = HttpContext.Current.IsDebuggingEnabled,
TokenEndpointPath = new PathString("/oauth2/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(90),
Provider = new CustomOAuthProvider(),
AccessTokenFormat = new CustomJwtFormat("http://example.com/")
};
现在有不同类型的应用程序使用API。对于网络客户端来说,90分钟的到期时间已经足够了,但对于移动应用来说,这太短了。
移动应用程序有一种方法可以在1年后获得令牌到期吗?我可以使用自定义HTTP标头来区分应用程序的类型。我试图在我的CustomJwtFormat类的Protect方法中延长expiration,这确实允许JWT中更大的到期时间。
public class CustomJwtFormat : ISecureDataFormat<AuthenticationTicket> {
public string Protect(AuthenticationTicket data) {
... emitted for brevity ...
string appId = HttpContext.Current.Request.Headers.GetValues("my-custom-header").FirstOrDefault();
if (appId == null)
throw new ApplicationException("Application ID header is missing");
if (appId.ToLower() == "mobileappheader") {
// set expiration to 1 year
expires = DateTimeOffset.UtcNow.AddYears(1);
}
var token = new JwtSecurityToken(issuer, audienceId, data.Identity.Claims,
issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingkey);
但在授权回复中,仍然说90分钟:
{
"access_token": "eyJ0eX...0CLY6jU",
"token_type": "bearer",
"expires_in": 5399
}
如您所见,expires_in
仍设置为90分钟的时间。
答案 0 :(得分:1)
虽然服务器的响应表明90分钟到期,但ASP.NET web api会查看故障单内部以确定到期时间。因此,如果我将其默认设置为90分钟(在startup.cs中)和我的移动应用程序设置为1年,那么我的移动应用程序将会在1年后到期。