Asp.net web api OAuth JWT访问令牌到期时间取决于客户端类型

Asp.net web api OAuth JWT访问令牌到期时间取决于客户端类型,asp.net-web-api,oauth-2.0,jwt,Asp.net Web Api,Oauth 2.0,Jwt,我基于创建了一个JWT令牌实现 以下代码已添加到我的Owin启动类中: OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { AllowInsecureHttp = HttpContext.Current.IsDebuggingEnabled, TokenEndpointPath = new PathString("/oauth2/token"),

我基于创建了一个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。对于web客户端来说,90分钟的过期时间已经足够了,但对于移动应用程序来说,这太短了

有没有办法让移动应用程序在1年后获得令牌到期?我可以使用自定义HTTP头来区分应用程序的类型。我尝试在CustomJwtFormat类的Protect方法中延长过期时间,这确实允许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);

如您所见,中的
expires\u仍然设置为90分钟时间跨度。

尽管来自服务器的响应指示90分钟到期,但ASP.NET web api会查看票据内部以确定到期时间。因此,如果我将我的移动应用程序的默认值设置为90分钟(在startup.cs中)和1年,那么我的移动应用程序将有1年的到期时间。

出于好奇,你是如何实现代币消费的?我遵循了相同的教程,但有点困惑,因为每次添加新用户时,应用程序似乎都需要重新启动。你找到解决这个问题的办法了吗?
{
    "access_token": "eyJ0eX...0CLY6jU",
    "token_type": "bearer",
    "expires_in": 5399
}