1

我试着用

> GzipCompressionProviderOptions

在服务中间件中,并得到错误消息:

“Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware:信息:承载未通过身份验证。失败消息:没有可用于令牌的 SecurityTokenValidator:未定义。”

这是我在 Middlewware 中的配置:

        ////Configure Compression level
        services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);

        //Add Response compression services
        services.AddResponseCompression(options =>
        {
            options.Providers.Add<GzipCompressionProvider>();
        });

并在配置中:

app.UseResponseCompression();

如果我删除配置,那么注册承载将被管理并创建。有谁知道为什么会发生这种情况。谢谢。

更新 :

这是我在中间件 ConfigureService 中的配置 JWtBearer :

        public void ConfigureServices(IServiceCollection services)
        {
            RSAParameters keyParams = RSAKeyUtils.GetKeyParameters("issuerToken.json");
            key = new Microsoft.IdentityModel.Tokens.RsaSecurityKey(keyParams);
            tokenOptions = new TokenAuthOptions()
            {
                Audience = TokenAudience,
                Issuer = TokenIssuer,
                SigningCredentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(key, Microsoft.IdentityModel.Tokens.SecurityAlgorithms.RsaSha256Signature)
            };}

and : 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            var tokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = key,
                ValidateIssuer = true,
                ValidIssuer = TokenIssuer,
                ValidateAudience = true,
                ValidAudience = TokenAudience,
                ValidateLifetime = true,
                ClockSkew = TimeSpan.FromMinutes(600)
            };

            app.UseJwtBearerAuthentication(new JwtBearerOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                TokenValidationParameters = tokenValidationParameters
            });

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                AuthenticationScheme = "Cookies",
                CookieName = "access_token",
                CookieSecure = CookieSecurePolicy.SameAsRequest,
                CookieHttpOnly = true,
                ExpireTimeSpan = TimeSpan.FromDays(1),
                AccessDeniedPath = "/Home/Index",
                LoginPath = "/AuthMember",
                LogoutPath = "/Home",
                Events = new CookieAuthenticationEvents
                {
                    OnRedirectToLogin = ctx =>
                    {
                        if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.OK)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                        else if (ctx.Request.Path.ToString().Contains("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.OK)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                        else if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.Unauthorized)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                        else if (ctx.Request.Path.ToString().Contains("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.Unauthorized)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                        else if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.Forbidden)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        }
                        else if (ctx.Request.Path.ToString().Contains("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.Forbidden)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                        }
                        else if (ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.NotFound)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
                        }
                        else if (ctx.Request.Path.ToString().Contains("/api") && ctx.Response.StatusCode == (int)HttpStatusCode.NotFound)
                        {
                            ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
                        }
                        else
                        {
                            ctx.Response.Redirect(ctx.RedirectUri);
                        }
                        return Task.FromResult(0);
                    }
                }
            });
4

0 回答 0