2
誰でも!ApsコアプロジェクトでCORSを有効にする際の問題
私はAsp.CoreプロジェクトでCORSを設定するのに苦労しています。
これは私のStartup.csファイルである:ここで
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Mvc.Cors.Internal;
namespace Api
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddJsonFormatters()
.AddAuthorization();
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.WithOrigins("http://localhost:54540").AllowAnyHeader().AllowAnyMethod().AllowCredentials());
}
);
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin"));
});
}
public void Configure(IApplicationBuilder app)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:54540",
RequireHttpsMetadata = false,
ScopeName = "api1",
AutomaticAuthenticate = true
});
app.UseMvc();
app.UseCors("AllowSpecificOrigin");
}
}
}
は私の要求です:
$.ajax({
type: "GET",
url: "http://localhost:1773/Claims/",
headers: {
Authorization: "Bearer " + getUrlParameter("access_token"),
},
xhrFields: {
withCredentials: true
}
}).done(function (data) {
alert(data);
});
私はこの応答を取得しています:
XMLHttpRequest cannot load http://localhost:1773/Claims/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:54540' is therefore not allowed access. The response had HTTP status code 500.
私は[EnableCors(追加されました"AllowSpecificOrigin")]をクリックします。
誰かが私にこれを手伝うことができれば非常に感謝します。
http://stackoverflow.com/questions/31942037/how-to-enable-cors-in-asp-net-5 – Oluwafemi