.NET JWT刷新令牌:如何处理检索新令牌时发出的请求

时间:2015-12-26 07:13:25

标签: c# jwt

我们希望将JSON Web Token(JWT)用作新API的API密钥。它们将在服务器应用程序级别使用,而不是最终用户在缓存中保留。它将允许我们在授权方面比基本身份验证更细化,而不会牺牲简单性。通过研究这个想法,我们发现了一个名为“Auth0”(https://auth0.com/)的服务正是这样做的,这似乎证实了我们的想法。

问题是我们似乎无法无限期地发布JWT,因为.NET“OAuthAuthorizationServerOptions”似乎迫使我们设置过期。我们已经读过“刷新令牌”,但经过几个小时的研究后,我们并不清楚刷新过程中发生的请求会发生什么。

e.g。如果客户端请求新令牌并且在检索新JWT所需的时间内正在进行其他请求,那么这些请求是否会失败?

我发现其他一些平台允许旧令牌在一分钟内保持活动状态以适应这种情况(例如这篇文章:https://laracasts.com/discuss/channels/general-discussion/how-to-refreshing-jwt-token),但同样,它似乎在.NET中不可用。

任何人都可以解释在.NET中刷新令牌的正确方法,但是具体说明了在检索新令牌并将其保存在客户端时遵守请求的问题吗?

使用代码更新我们的问题......

令牌生成:

private void OAuthTokenGeneration(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            //For Dev enviroment only (on production should be AllowInsecureHttp = false)
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/oauth/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(365),
            Provider = new CustomOAuthProvider(),
            AccessTokenFormat = new CustomJwtFormat(ConfigurationManager.AppSettings["as:live"])
        };

        // OAuth 2.0 Bearer Access Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
    }

...授权

        private void OAuthTokenConsumption(IAppBuilder app)
    {
        var issuer = ConfigurationManager.AppSettings["as:live"];
        string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];
        byte[] audienceSecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["as:AudienceSecret"]);

        // Api controllers with an [Authorize] attribute will be validated with JWT
        app.UseJwtBearerAuthentication(
            new JwtBearerAuthenticationOptions
            {
                AuthenticationMode = AuthenticationMode.Active,
                AllowedAudiences = new[] { audienceId },
                IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                {
                    new SymmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret)
                }
            });
    }

1 个答案:

答案 0 :(得分:2)

令牌应在到期之前刷新,而不是在之后刷新。旧令牌和新令牌都有效,直到它们最终到期。在这种情况下,在刷新时使用旧令牌发出的请求将正常工作。

这是我的建议,而不是使用过期的JWT +刷新令牌:

OAuthAuthorizationServerOptions与JWT身份验证无关。我相信你正在寻找JwtBearerAuthenticationOptions。此JWT中间件仅验证配置的参数,如受众,签名等,但不会发出令牌,因此无需配置到期时间。

Auth0的API使用JWT进行身份验证,但没有服务可以发出这些令牌。相反,令牌由API的任何授权使用者通过使用共享API密钥创建,该秘密用于对令牌进行签名。例如,令牌可以在浏览器上本地生成,并且使用API Explorer没有到期日期。还有一个端点可以通过ID(jti声明)对令牌进行黑名单,以防它们遭到入侵或不再需要。

安全方面可以说这实际上相当于拥有一个即将到期的JWT +刷新令牌,因为无论如何你需要一种方法来将受损的刷新令牌列入黑名单。它也更容易理解,不使用不透明的令牌,并且使得不那么繁琐的API。

相关问题