c# - 当请求的凭证模式为 'Access-Control-Allow-Origin' 时,响应中 '*' header 的值不得为通配符 'include'

标签 c# angular asp.net-web-api asp.net-core cors

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

问题是

Failed to load http://localhost:52487/api/Authentication/: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

我在前端使用 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();
}

这就是我从 UI( Angular )端调用 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);
    });
}

最佳答案

当我在 AddPolicy 中调用 AllowCredentials() 时,它正在工作

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

我从 Access-Control-Allow-Origin: "*" not allowed when credentials flag is true, but there is no Access-Control-Allow-Credentials header

我的理解

I am using { withCredentials: true } in angular http service call. So I guess I should use AllowCredentials() policy in CORS service.

关于c# - 当请求的凭证模式为 'Access-Control-Allow-Origin' 时,响应中 '*' header 的值不得为通配符 'include',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48861290/

相关文章:

twitter-bootstrap - ng-bootstrap 不工作(Angular2)

AngularJS 与 Angular

c# - 设置默认全局 json 序列化程序设置

asp.net-mvc - IIS劫持CORS Preflight OPTIONS请求

c# - 如果将委托(delegate)定义放在另一个项目中,编译会失败吗?

c# - 一个对象订阅自己的事件改变内部数据可以吗?

c# - C++加密,C#解密=失败

javascript - Angular 5 - HttpClient XML Post - 解析期间 Http 失败

json - 如何形成 ASP.NET OData WebApi 的 PATCH 正文?

c# - 如何将现有表单添加到新项目?