我正在使用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)