承载error =“ invalid_token”,error_description =“签名无效”

Tho*_*ter 5 azure-active-directory .net-core asp.net-core-webapi asp.net-core-2.0

我有一个角度的应用程序,要求从天蓝色的令牌。登录进展顺利,我得到了令牌。现在,此令牌已从角度应用程序发送到网络核心webapi应用程序。网络核心应验证此令牌,但失败。我认为webapi还应该联系azure来验证令牌,因为它不知道验证令牌所需的私钥和公钥。

目前尚不清楚为什么它会失败。angular应用程序和webapi都在我的计算机上本地运行。

错误是: Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException: 'IDX10500: Signature validation failed. No security keys were provided to validate the signature.'

我的净核心2配置是:

var tokenValidationParameters = new TokenValidationParameters
            {

                RequireExpirationTime = true,
                RequireSignedTokens = false,
                ValidateIssuerSigningKey = true,
                ValidateIssuer = true,
                ValidIssuer = "8d708afe-2966-40b7-918c-a39551625958",
                ValidateAudience = true,
                ValidAudience = "https://sts.windows.net/a1d50521-9687-4e4d-a76d-ddd53ab0c668/",
                ValidateLifetime = false,
                ClockSkew = TimeSpan.Zero
            };
            services.AddAuthentication(options =>
            {
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

            }).AddJwtBearer(options =>
            {

                options.Audience = "8d708afe-2966-40b7-918c-a39551625958";
                options.ClaimsIssuer = "https://sts.windows.net/a1d50521-9687-4e4d-a76d-ddd53ab0c668/";
                options.RequireHttpsMetadata=false;
                options.TokenValidationParameters = tokenValidationParameters;
                options.SaveToken = true;
            });
Run Code Online (Sandbox Code Playgroud)

juu*_*nas 9

你有很多配置:)

两个强制性设置是受众和权威:

services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(o =>
    {
        o.Audience = "8d708afe-2966-40b7-918c-a39551625958";
        o.Authority = "https://login.microsoftonline.com/a1d50521-9687-4e4d-a76d-ddd53ab0c668/";
    });
Run Code Online (Sandbox Code Playgroud)

您缺少授权,因此它不知道从哪里加载签名公钥。

  • 您还可以使用 TokenValidationParameters.ValidAudiences 添加其他受众 URL。例如,当调用者使用identifierUris作为范围来请求令牌时,默认的受众检查将失败,因为受众是应用程序的App Id。```c# options.TokenValidationParameters.ValidAudiences = new string[] { "[identifierUris]" }; ```` (2认同)

Mil*_*vic -1

You are missing IssuerSigningKey property in your TokenValidationParameters. Thats why its complaining.

The simplest example would be

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("yOURsECRETkEY12345"))
Run Code Online (Sandbox Code Playgroud)

I'm not sure how azure comes into play, you probably need it to retrieve security key information, if thats your signing authority

Edit:

Azure specific settings

.AddJwtBearer(options => {
        options.Authority = string.Format("https://login.microsoftonline.com/tfp/{0}/{1}/v2.0/", Configuration["Authentication:AzureAd:Tenant"], Configuration["Authentication:AzureAd:Policy"]);
        options.Audience = Configuration["Authentication:AzureAd:ClientId"];
    });
Run Code Online (Sandbox Code Playgroud)