在Middlewear中使用GzipCompressionProviderOptions,Bearer未经过身份验证

时间:2016-11-20 09:27:22

标签: asp.net-core asp.net-core-mvc middleware asp.net-core-1.0 asp.net-core-middleware

我尝试使用

> 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();

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

更新:

这是我在中间件配置服务中的配置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);
                    }
                }
            });

0 个答案:

没有答案