1

I am getting cors issue while i am migrating from .net core 2.2 to .net core 3.0

  public void Configure(IApplicationBuilder app, IHostEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime applicationLifetime)
        {
            NLog.LogManager.Configuration = new NLogLoggingConfiguration(Configuration.GetSection("NLog"));
            LogManager.Configuration.Variables["connectionString"] = Encryptor.GetNLogDB();
            LogManager.Configuration.Variables["ApplicationName"] = env.ApplicationName;
            LogManager.Configuration.Variables["EnvironmentType"] = env.EnvironmentName;
            app.UseStaticFiles();
            app.UseRouting();
            app.UseCors("AllowAll");

            applicationLifetime.ApplicationStopping.Register(OnShutdown);
            applicationLifetime.ApplicationStarted.Register(OnStarted);

            app.UseAuthentication();
            app.UseAuthorization();
            app.UseLogAndExceptionHandler();

            //app.UseCors();
            //app.UseSignalR(routes =>
            //{
            //    routes.MapHub<ApplicationHub>(SignalRHub);
            //    routes.MapHub<QuillHub>(QuillHub);
            //});
            app.UseEndpoints(endpoints => {
                endpoints.MapControllers();
            });
            //app.UseMvc();

            Initialize(app);
        }

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

While running this i am getting an error saying i cant use AllowAnyOrigin and AllowCredentials together and when i remove any of those i get run time error has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

I want to allow all origins.

Harsha Mullangi
  • 474
  • 1
  • 6
  • 27

1 Answers1

3

AllowAnyOrigin and AllowCredentials could not be used together based on CORS doc.

Try to use SetIsOriginAllowed as a workaround

services.AddCors(options =>
        {
            options.AddPolicy("AllowAll",
                builder => builder.AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .SetIsOriginAllowed(_ => true)
                                  .AllowCredentials());
        });
Ryan
  • 19,118
  • 10
  • 37
  • 53