2016-06-13 4 views
0

私は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(); 
    } 

}

答えて

0

それはあなたのトークンミドルウェアが着信要求を検証するために実行されていないようです。 トークンミドルウェアを自動的に実行するように設定してみてください。

var options = new JwtBearerOptions 
    { 
     //other configurations.. 
     AutomaticAuthenticate = true; 
    }; 

属性を使用して、コントローラで認証方式を指定することもできます。

[Authorize(AuthenticationSchemes = "MyAuthenticationScheme")] 

ここでそれについて詳しく読む:Limiting identity by scheme

+0

AutomaticAuthenticate = true;デフォルトです – TealFawn

0

問題がConfigureServicesセクションのポリシーとありました。最も簡単な方針は私が今必要とするものです。

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddMvc(c => 
     { 
      // TODO implement this abstract class c.Filters.Add(typeof(ExceptionFilterAttribute)); 
      var policy = new AuthorizationPolicyBuilder() 
       .RequireAuthenticatedUser() 
       .Build(); 
      c.Filters.Add(new AuthorizeFilter(policy)); 
      c.Filters.Add(typeof(ValidateModelFilter)); 
     }); 
関連する問題