私はasp.netの地獄を構成するときに私が得るautomagicに掘り下げる方法を理解しようとしています。私は現在asp.net web-api 2からasp.netコアに小さなapiを翻訳しています。私はこの構成で403がどこから来ているのか、それを修正する方法がわからない。現在、apiエンドポイントの大半は有効なトークンを必要とし、トークン内の特定のクレームを確認する必要はありません。したがって、私の認証されたすべてのコントローラに対して、有効なベアラトークンを使用するときには、200でなければならない403の応答が得られます。また、私はプロバイダとしてAuth0で非対称キーを使用しています。JwtBearerAuthenticationを使用して403を修正するにはどうすればよいですか?
Startup.cs configureメソッドJWTベアラトークンの検証に使用しています。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//Middleware added here order matters
//TODO formatter settings https://docs.asp.net/en/latest/mvc/models/formatting.html
//samples to check
//https://auth0.com/docs/server-apis/webapi-owin
//https://github.com/auth0-samples/auth0-aspnetcore-webapi-rs256
var options = new JwtBearerOptions
{
Audience = Configuration["auth0:clientId"]
,Authority = $"https://{Configuration["auth0:domain"]}/"
,Events = new JwtBearerEvents() // just a pass through to log events
};
app.UseJwtBearerAuthentication(options);
// Very hacky to catch invaild tokens https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/issues/191
// issue says the need for the required hack is fixed but it's been still happening. Issue about the fix https://github.com/aspnet/Security/issues/411
app.Use(next => async context => {
try
{
await next(context);
}
catch
{
// If the headers have already been sent, you can't replace the status code.
// In this case, throw an exception to close the connection.
if (context.Response.HasStarted)
{
throw;
}
context.Response.StatusCode = 401;
}
});
app.UseMvc();
// TODO global exception handling https://github.com/dotnet/corefx/issues/6398
app.UseSwaggerGen();
app.UseSwaggerUi();
}
}
AutomaticAuthenticate = true;デフォルトです – TealFawn