Asp.net core 为什么JWT颁发者签名密钥验证总是返回有效?

Asp.net core 为什么JWT颁发者签名密钥验证总是返回有效?,asp.net-core,jwt,openid-connect,Asp.net Core,Jwt,Openid Connect,我有一个Google发布/订阅推送订阅,它向端点发送JWT令牌。端点需要验证此令牌。从谷歌文档中,我需要检查发行人、受众和签名。这很好,除了我添加到IssuerSigningKey的内容之外,令牌是有效的。我希望每次拔出钥匙的一部分时,它都会断开 我为IssuersingKey和IssuersingKey尝试了各种不同的值。不管怎样,我都得到了有效的答复。更改域或访问群体参数会返回未经授权的消息 public void ConfigureServices(IServiceCollection s

我有一个Google发布/订阅推送订阅,它向端点发送JWT令牌。端点需要验证此令牌。从谷歌文档中,我需要检查发行人、受众和签名。这很好,除了我添加到IssuerSigningKey的内容之外,令牌是有效的。我希望每次拔出钥匙的一部分时,它都会断开

我为IssuersingKey和IssuersingKey尝试了各种不同的值。不管怎样,我都得到了有效的答复。更改域或访问群体参数会返回未经授权的消息

public void ConfigureServices(IServiceCollection services)
{
    string domain = "https://accounts.google.com";
    string audience = "theaudience";
    // Just to debug/test
    string signingKey = "---- - BEGIN PRIVATE KEY-----\nMIIfujHGitJ\n---- - END PRIVATE KEY-----\n";
    var certificates = 
    this.FetchGoogleCertificates().GetAwaiter().GetResult();

        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            options.Authority = domain;
            options.Audience = audience;

            options.TokenValidationParameters = new TokenValidationParameters
            {
                ClockSkew = TimeSpan.FromHours(48), // This is just for debugging. Maybe we do need a little clock skew if the clock in Google is not aligned with the VD system
                ValidateAudience = true, // Validate the audience, this will change in production to the endpoint URL
                ValidateIssuer = true, // Validate the issuer (Google). If this is wrong, we get a 500 error instead of 40x
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(signingKey)),
                /* Stuff I also tried:
                IssuerSigningKey = new RsaSecurityKey(new RSACryptoServiceProvider(2048))
                IssuerSigningKeys = certificates.Values.Select(x => new X509SecurityKey(x)),
                IssuerSigningKeyResolver = (token, securityToken, kid, validationParameters) =>
                {
                    return certificates
                    .Where(x => x.Key.ToUpper() == kid.ToUpper())
                    .Select(x => new X509SecurityKey(x.Value));
                }
                */
            };
        });

        services.AddAuthorization();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }
这里发生了什么?

来自:

首先,不应在上设置权限属性 JwtBearerOptions。如果设置了,中间件会假定它可以运行 访问该URI以获取令牌验证信息

在代码示例中,您将保留Authority属性集,这将导致验证库向accounts.google.com发出网络请求,以获取令牌验证信息。如果您未设置Authority属性,它将被迫使用您的TokenValidationParameters。

来自:

首先,不应在上设置权限属性 JwtBearerOptions。如果设置了,中间件会假定它可以运行 访问该URI以获取令牌验证信息


在代码示例中,您将保留Authority属性集,这将导致验证库向accounts.google.com发出网络请求,以获取令牌验证信息。如果您未设置Authority属性,它将被迫使用您的TokenValidationParameters。

有效。给其他人一个提示:我本以为必须使用属于谷歌云服务帐户的证书,但这不起作用。相反,我不得不使用通用的谷歌证书。给其他人一个提示:我本以为必须使用属于谷歌云服务帐户的证书,但这不起作用。相反,我不得不使用通用的谷歌证书