相关疑难解决方法(0)

AddJwtBearer OnAuthenticationFailed返回自定义错误

我正在使用Openidict.
我试图返回自定义状态代码的自定义消息,但我无法做到.我的配置在startup.cs:

services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(o =>
            {
                o.Authority = this.Configuration["Authentication:OpenIddict:Authority"];
                o.Audience = "MyApp";           //Also in Auhorization.cs controller.
                o.RequireHttpsMetadata = !this.Environment.IsDevelopment();
                o.Events = new JwtBearerEvents()
                {
                    OnAuthenticationFailed = context =>
                    {
                        context.Response.StatusCode = HttpStatusCodes.AuthenticationFailed;
                        context.Response.ContentType = "application/json";
                        var err = this.Environment.IsDevelopment() ? context.Exception.ToString() : "An error occurred processing your authentication.";
                        var result = JsonConvert.SerializeObject(new {err});
                        return context.Response.WriteAsync(result);
                    }
                };
            });
Run Code Online (Sandbox Code Playgroud)

但问题是没有返回内容.Chrome开发者工具报告

(失败)

错误

状态和

无法加载响应数据

错误

回应.

我也尝试过:

context.Response.WriteAsync(result).Wait();
return Task.CompletedTask; …
Run Code Online (Sandbox Code Playgroud)

asp.net-core openiddict asp.net-core-2.0

2
推荐指数
3
解决办法
7735
查看次数