How to enable CORS globally in ASP.NET web API core

Luis Valencia

I found this site:

https://docs.microsoft.com/en-us/aspnet/core/security/cors

However I am confused in how to enable it globally, because it seems there are 2 ways to do it, whats the difference between these 2 ways? or they do 2 different things?

public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //https://docs.microsoft.com/en-us/aspnet/core/security/cors
            services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigin",
                    builder => builder.WithOrigins("http://example.com")
                                        .AllowAnyHeader()
                    );
            });

            services.Configure<MvcOptions>(options =>
            {
                options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin"));
            });

            // Add framework services.
            services.AddMvc();
        }

        // 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.UseCors("AllowSpecificOrigin");

            app.UseMvc();
        }
Shawn Wildermuth

The calls in the ConfigureServices just adds Cors services, not set it up (including creating hte policy). By adding the filter you're making it global, but I understand that the UseCors (in Configure) is the preferred way to add it globally. All that the Filter code does is force the attribute on all controllers, and the UseCors effectively does the same thing, just in a different part of the stack. I believe the UseCors will do it for more than just MVC calls which is why it's different.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to enable CORS in ASP.net Core WebAPI

Enable OPTIONS header for CORS on .NET Core Web API

How to enable CORS in ASP.NET Core

Enable CORS for Web API 1, .net 4.0

How to globally handle exceptions in asp.net web api when exception is raised through lambda expression

How do you enable cross-origin requests (CORS) in ASP.NET Core MVC

How to enable OData in ASP.Net 5 Web API

CORS and ASP.Net Web API

How to enable CORS globally in my ASP.NET core WebApi project

How do I enable BSON serialization in my ASP.NET Core Web API?

Enable CORS on Specific URL in ASP.NET Web Forms

Browser ignoring set-cookie header from CORS response (asp.net core 2.0 web api)

Asp.net core web api using windows authentication - Cors request unauthorised

Angular 6 Asp.Net (not Core) Web Api CORS request fails

How to enable CORS for a local file that references a hosted ASP.NET Web API that is hosted on Amazon AWS

How to enable CORS in asp.net Self-Hosted API?

How to enable LZ4 compression for messagepack content type in Asp.net Core Web API

Enable CORS on a .NET Core Api?

Can't enable CORS for specific API controllers in ASP.NET Core WebApp targeting netcoreapp3.0

How to correctly enable CORS for a subdomain in an ASP.NET Core 2.2 project?

How to enable Cors for every type of request in asp.net core 3.1

I am migrating my asp.net web api to asp.net core. Cors migration

How to enable cors in .Net Core for web api ajax call

How to enable CORS in Asp.Net Core 3.0 WebAPI

Enable CORS on an ASP.NET core MVC project

CORS blocking post request even though CORS is enabled in configuration ASP.NET Core Web API

How to enable cors in ASP.NET Core 6.0 Web API project?

ASP.NET core web api middleware overriding cors

.Net Core 6 web api Cors

TOP Ranking

HotTag

Archive