【问题标题】:Read and Validate JWT token per request每个请求读取和验证 JWT 令牌
【发布时间】:2016-03-29 06:44:16
【问题描述】:

我有一个场景,其中有许多单独的客户端通过 JWT 令牌连接。

  1. 客户端(浏览器)首先需要登录(并获得 JWT 令牌)
  2. 然后客户端需要检索他们的帐户信息,他们通过向服务器(包括 JWT 令牌)发送请求来完成此操作。服务器(有权访问密钥)读取 JWT 令牌(安全)并应发送回用户信息,怎么办?

附言每个客户都有不同的秘密

我可以针对每个应用执行此操作

app.UseJwtBearerAuthentication(
                new JwtBearerAuthenticationOptions
                {
                    AllowedAudiences = new[] { audience },
                    IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[]
                    {
                        new SymmetricKeyIssuerSecurityTokenProvider(issuer, secret)
                    },
                    Provider = new CookieOAuthBearerProvider("authCookie")
                });

但此方法不适用于每个请求....

【问题讨论】:

  • 你能改写你的问题吗?当您说“我需要向我的服务器发送请求”时,您的意思是您的应用程序服务器需要将 JWT 令牌传递给单独的服务器吗?或者您是在谈论将令牌从客户端发送到您的服务器?
  • 希望能解决这个问题。
  • 会话 cookie 不够好是有原因的吗?如果您在控制器方法上添加 Authorize 属性,则可以确保用户已正确验证(在本例中使用 JWT 令牌)。无论如何,您可以使用 JwtSecurityTokenHandler.ValidateToken 验证令牌
  • 是的,原因是因为这是一个 SSO 系统,JWT 令牌被传递给另一个完全独立的应用程序。此外,我连接的服务器已经包含它自己的登录系统。

标签: c# jwt


【解决方案1】:

这是我们当前使用的 sn-p(连接到 AzureAD)。 您需要实现 GetSigningCertificates,它返回 IEnumerable<X509SecurityToken> 以验证 JWT 是否已正确签名。

internal static ClaimsPrincipal GetClaimPrincipalFromToken(string jwtSecurityHeader)
{
    var jwtSecurityHandler = new JwtSecurityTokenHandler();

    var signingCertificates = GetSigningCertificates(ConfigHelper.FederationMetadataDocument);
    var tokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidAudience = ConfigHelper.AppIdURI,
        ValidIssuer = ConfigHelper.Issuer,
        LifetimeValidator =
            (before, expires, token, parameters) =>
            {
                //Don't allow not-yet-active tokens
                if (before.HasValue && before.Value > DateTime.Now)
                    return false;

                //If expiration has a date, add 2 days to it
                if (expires.HasValue)
                    return expires.Value.AddDays(2) > DateTime.Now;

                //Otherwise the token is valid
                return true;
            },
        ValidateLifetime = true,
        IssuerSigningTokens = signingCertificates,
    };

    var headerParts = jwtSecurityHeader.Split(' ');
    if (headerParts.Length != 2 || headerParts[0] != "Bearer")
        throw new AuthorizationException(HttpStatusCode.Forbidden, "Invalid token type");

    var jwtSecurityToken = headerParts[1];
    SecurityToken jwtToken;
    var claimsPrincipal = jwtSecurityHandler.ValidateToken(jwtSecurityToken, tokenValidationParameters, out jwtToken);

    return claimsPrincipal;
}

您需要针对您的应用程序对其进行一些调整,但这应该可以帮助您完成大部分工作。请注意,此代码正在解析 {HeaderType} {Token} 格式(例如 Bearer {token})。如果您只是解析{Token},则需要删除.Split(' ')

【讨论】:

    猜你喜欢
    • 2021-09-17
    • 2019-04-02
    • 2020-06-08
    • 2020-11-09
    • 2019-09-16
    • 2017-10-15
    • 2020-09-12
    • 2017-09-24
    • 2021-08-16
    相关资源
    最近更新 更多