Angular(4)からAsp NetコアAPIへのベアラトークンを使用してリクエストを行うのが苦労しています。アングル4からasp.netコアAPIへのリクエスト(ヘッダーでの許可を使用)が動作していません
マイ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();
}
しかし、私は不正なエラーを取得してください!
私はStackOverflowの(.NET Core UseCors() does not add headers、Enable OPTIONS header for CORS on .NET Core Web APIとHow to disable OPTIONS request?)に主にここで、私がオンライン読んだ多くのソリューションを試してみましたが、それは麦汁に得ることができませんでした! :(私は郵便配達を使用して要求を行うと、それはに動作し 。