2017-08-18 8 views
1

Angular(4)からAsp NetコアAPIへのベアラトークンを使用してリクエストを行うのが苦労しています。アングル4からasp.netコアAPIへのリクエスト(ヘッダーでの許可を使用)が動作していません

enter image description here

マイAPI Startup.csコード:ここで

は、私は、角度からそれをやっている方法です

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddMvc(); 
    services.AddAuthentication(); 

    services.AddCors(options => 
    { 
    options.AddPolicy("CorsPolicy", 
     builder => builder.AllowAnyOrigin() 
     .AllowAnyMethod() 
     .WithHeaders("authorization", "accept", "content-type", "origin")); 
    }); 

    // Add the configuration singleton here 
    services.AddSingleton<IConfiguration>(Configuration); 

} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    app.UseCors("CorsPolicy"); 
    app.UseOptions(); 
    app.Use(async (context, next) => 
    { 
    await next(); 
    if (context.Response.StatusCode == 404 && 
     !Path.HasExtension(context.Request.Path.Value) && 
     !context.Request.Path.Value.StartsWith("/api/")) 
    { 
     context.Request.Path = "/index.html"; 
     await next(); 
    } 
    }); 
    app.UseExceptionHandler(errorApp => 
    { 
    errorApp.Run(async context => 
    { 
     context.Response.StatusCode = 500; // or another Status accordingly to Exception Type 
     context.Response.ContentType = "application/json"; 

     var error = context.Features.Get<IExceptionHandlerFeature>(); 
     if (error != null) 
     { 
     var ex = error.Error; 

     await context.Response.WriteAsync(ex.Message, Encoding.UTF8); 
     } 
    }); 
    }); 

    var jwtAuth = new JwtBearerOptions 
    { 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true, 
    Authority = $"{Configuration["Authentication:AzureAd:AADInstance"]}{Configuration["Authentication:AzureAd:TenantId"]}", 
    Audience = Configuration["Authentication:AzureAd:ClientId"], 
    TokenValidationParameters = 
      new Microsoft.IdentityModel.Tokens.TokenValidationParameters 
      { 
       ValidIssuer = Configuration["Authentication:AzureAd:Issuer"] 
      } 
    }; 
    app.UseJwtBearerAuthentication(jwtAuth); 


    app.UseMvcWithDefaultRoute(); 
    app.UseDefaultFiles(); 
    app.UseStaticFiles(); 
} 

しかし、私は不正なエラーを取得してください!

enter image description here
enter image description here

私はStackOverflowの(.NET Core UseCors() does not add headersEnable OPTIONS header for CORS on .NET Core Web APIHow to disable OPTIONS request?)に主にここで、私がオンライン読んだ多くのソリューションを試してみましたが、それは麦汁に得ることができませんでした! :(私は郵便配達を使用して要求を行うと、それはに動作し 。

答えて

1

OK OK .. ..私はこれを理解しようとしているので、多くの時間を費やしたことをどのようにひどい間違いを信じることができない! コンピュータの画面と椅子の間に問題がありました。間違った角度サービスでAuthorizationのヘッダーを追加しようとしていました!!!

関連する問題