当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不得为通配符“ *”

Ram*_*ran 3 c# cors asp.net-web-api asp.net-core angular

我的应用程序在IE浏览器中运行良好,但是由于问题它在Chrome浏览器中无法运行CORS

问题是

无法加载http:// localhost:52487 / api / Authentication /:当请求的凭据模式为“ include”时,响应中“ Access-Control-Allow-Origin”标头的值不能为通配符“ *” 。因此,不允许访问源' http:// localhost:4200 '。XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制。

我在前端使用angular 2,在后端使用Asp.net core 1.0。我试过了

这是我的启动代码

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AllowAll", p =>
        {
            p.AllowAnyOrigin()
            .AllowAnyHeader()
            .AllowAnyMethod();
        });
    });

    // Add framework services.
    services.AddMvc();
    // Add functionality to inject IOptions<T>
    services.AddOptions();
    // Add our Config object so it can be injected
    services.Configure<Data>(Configuration.GetSection("Data"));

    services.Configure<COCSettings>(Configuration.GetSection("COCSettings"));

    services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));

    AppSettings.ConnectionString = Configuration["Data:DefaultConnectionString"];

    // *If* you need access to generic IConfiguration this is **required**
    services.AddSingleton<IConfiguration>(Configuration);

    // Injecting repopsitories with interface
    AddServices(services);

    // Add Json options
    services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}

// 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();
    app.UseMiddleware(typeof(ErrorHandling));
    app.UseMiddleware(typeof(GetNoCache));
    app.UseCors("AllowAll");
    app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)

这就是我从UI(角度)端调用API的方式

constructor(private http: Http) {
    this.headers = new Headers();
    this.headers.append('Accept', 'application/json');
}

GetMaintainCOC(FYONId) {
    return this.http.get(this.apiUrl + 'GetCertificationofConformity?FYONId=' + FYONId, { withCredentials: true })
    .map(responce => <any>responce.json())
    .catch(error => {
        return Observable.throw(error);
    });
}
Run Code Online (Sandbox Code Playgroud)

Ram*_*ran 6

当我在AllowCredentials()内部打电话时,它正在工作AddPolicy

 services.AddCors(options =>
            {
                options.AddPolicy("AllowAll", p =>
                {
                    p.AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
                });
            });
Run Code Online (Sandbox Code Playgroud)

我从Access-Control-Allow-Origin获得了这个想法的关键 :当凭据标记为true时不允许使用“ *”,但是没有Access-Control-Allow-Credentials标头

我的理解

{ withCredentials: true }在角度http服务电话中使用。所以我想我应该AllowCredentials()CORS服务中使用策略。

  • AllowAnyOrigin 不能与AllowCredentials 一起使用。需要有特定的起源 (4认同)