0

ASP.NETコアRC2でベアラ認証を使用しようとしています。それはユーザーauthenticadで動作し、役割を持っていますが、ユーザーが認証されていない(認証されているが、役割を持っていない)とき、私は403の代わりに404エラーが発生します。403の代わりにASP.NETコアRC2 404でベアラ認証

Startup.cs app.UseIdentity()を使用して

public void ConfigureServices(IServiceCollection services) 
    { 

     services.AddCors(options => 
     { 
      options.AddPolicy("CorsPolicy", 
       builder => 
       { 
        builder 
         .WithOrigins("*") 
         .AllowAnyHeader() 
         .AllowAnyMethod() 
         .AllowCredentials(); 
       } 
      ); 
     }); 

     services.AddIdentity<AppUser, AppRole>().AddEntityFrameworkStores<AppIdentityDbContext, int>(); 

     services.AddAuthorization(); 

     services.AddMvc(config => { 
      var policy = new AuthorizationPolicyBuilder() 
       .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) 
       .RequireAuthenticatedUser() 
       .Build(); 
      config.Filters.Add(new AuthorizeFilter(policy)); 
     }).AddJsonOptions(options => 
      options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver() 
     ); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseDatabaseErrorPage(); 
      app.UseBrowserLink(); 
     } 
     else 
     { 
      app.UseExceptionHandler("/home/error"); 
     } 

     app.UseStaticFiles(); 


     var signingKey = GetSigningKey(); 

     app.UseJwtBearerAuthentication(new JwtBearerOptions() 
     { 
      AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme, 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true, 
      TokenValidationParameters = new TokenValidationParameters() 
      { 
       IssuerSigningKey = signingKey, 
       ValidateIssuerSigningKey = true, 
       ValidateLifetime = true, 
       ValidAudience = "MyAudience", 
       ValidIssuer = "MyIssuer" 
      } 
     }); 

     app.UseCors(config => 
     { 
      config.AllowCredentials(); 
      config.AllowAnyOrigin(); 
      config.AllowAnyHeader(); 
      config.AllowAnyMethod(); 
     }); 

     app.UseIdentity(); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 

    public static SecurityKey GetSigningKey() 
    { 
     var plainTextSecurityKey = "This is my shared, not so secret, secret!"; 
     return new SymmetricSecurityKey(Encoding.UTF8.GetBytes(plainTextSecurityKey)); 
    } 

答えて

3

は、アプリケーションにCookieAuthenticationが追加されますので、すべての未認証の要求が/Account/Loginにリダイレクトされます。

それはあなたに404

ソース与えたので、おそらく、あなたがこれを処理する任意のルートを追加していない:(位置app.UseIdentityを確認してくださいhttps://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/BuilderExtensions.cs

+0

はい、これはいくつかのようでした。 configから 'app.UseIdentity()'に 'AutomaticChallenge = false'を変更しました。 – iuristona

0

を)もMVCルーティングapp.UseMvc()。 authenicateコードは、app.useIdenetity()とMvc rotningの下にある必要があります。次のようにします:app.UseApplicationInsightsExceptionTelemetry();

 app.UseStaticFiles(); 
     app.UseIdentity(); 


     app.UseCors(builder => 
      builder.AllowAnyOrigin() 
      .AllowAnyHeader() 
      .AllowAnyMethod() 
      ); 



     app.UseSwagger(); 
     app.UseSwaggerUi(); 

     ConfigureAuth(app); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "index"); 
     }); 
関連する問題