How to fix “The CORS protocol does not allow specifying a wildcard (any) origin and credentials at the same time” error

后端 未结 7 991
再見小時候
再見小時候 2021-02-03 17:10

I\'ve already enabled CORS on the project in C# .net Core

In startup.cs I\'ve added lines

...
services.AddCors();
...
app.UseCors(builder =         


        
7条回答
  •  悲哀的现实
    2021-02-03 17:45

    It's little bit late, but I hope it could be helpful for someone.

    If you want AllowCredentials() and AllowAnyOrigin() together just use SetIsOriginAllowed(Func predicate)

    doc about IsOriginAllowed

            services
                .AddCors(options =>
                {
                    options.AddPolicy("CorsPolicy",
                        builder => builder
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        );
    
                    options.AddPolicy("signalr",
                        builder => builder
                        .AllowAnyMethod()
                        .AllowAnyHeader()
    
                        .AllowCredentials()
                        .SetIsOriginAllowed(hostName => true));
                });
    

提交回复
热议问题