Monday-Friday, 9:00-17:00
 

CORE3.1 add CORS support

feature image

CORE3.1 add CORS support

Setting up CORS

To setup CORS for your application you use the Microsoft.AspNetCore.Cors package. In your project.json file, add the following:

  "dependencies": {
    "Microsoft.AspNet.Cors": "6.0.0-rc1-final",
  },

Add the CORS services in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
}

Enabling CORS with middleware

To enable CORS for your entire application add the CORS middleware to your request pipeline using the UseCors extension method. Note that the CORS middleware must precede any defined endpoints in your app that you want to support cross-origin requests (ex. before any call to UseMvc).

You can specify a cross-origin policy when adding the CORS middleware using the CorsPolicyBuilder class. There are two ways to do this. The first is to call UseCors with a lambda:

public void Configure(IApplicationBuilder app)
{
    app.UseCors(builder =>
        builder.WithOrigins("http://example.com"));
}

The lambda takes a CorsPolicyBuilder object. I’ll describe all of the configuration options later in this topic. In this example, the policy allows cross-origin requests from “http://example.com” and no other origins.

Note that CorsPolicyBuilder has a fluent API, so you can chain method calls:

app.UseCors(builder =>
    builder.WithOrigins("http://example.com")
           .AllowAnyHeader()
    );

The second approach is to define one or more named CORS policies, and then select the policy by name at run time.

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AllowSpecificOrigin",
            builder => builder.WithOrigins("http://example.com"));
    });
}

public void Configure(IApplicationBuilder app)
{
    app.UseCors("AllowSpecificOrigin");
    app.Run(async (context) =>
    {
        await context.Response.WriteAsync("Hello World!");
    });
}

PS: app.UseCors() should be set between Routing and Endpoints

configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(…).

https://jakeydocs.readthedocs.io/en/latest/security/cors.html#setting-up-cors

13 Comments

Awesome tool for creating a global business directory! Adding new cities is as easy as it can get, the extensive backend features for admin makes this theme a clear winner in this niche. Winnah Cleveland Ethbinium

Hey there. I discovered your blog by means of Google even as looking for a similar subject, your site came up. It seems great. I have bookmarked it in my google bookmarks to visit then. Eba Ogden Blodget

Great blog here! Also your website loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol Gayleen Dell Schiro

You certainly are a world traveler!! Again, I wish I had your spirit of independence and adventure, but then again not so much!!! Love reading your blog and living vicariously!! Safe travels and adventures but not tooooo exciting!!! Shanna Boycie Janerich

Great review. This sounds like a really intriguing and powerful book with a lot of interesting takeaways. Harrie Hilarius Angelita

I am not sure the place you are getting your info, but good topic. I must spend a while studying much more or figuring out more. Thank you for fantastic info I used to be in search of this information for my mission. Keslie Robbert Weslee

I loved as much as you will receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this increase. Leshia Duane Melburn

I was really delighted to discover this web-site. I wished to thanks for your time for this remarkable read!! I most definitely taking pleasure in every bit of it as well as I have you bookmarked to look into new stuff you article. Joana Rad Trisa

Having read this I thought it was rather informative. I appreciate you taking the time and energy to put this short article together. I once again find myself spending a lot of time both reading and leaving comments. But so what, it was still worth it! Hatty Darren Cadmarr

Leave a Reply to canli Cancel reply

Your email address will not be published. Required fields are marked *

Post navigation

  Next Post :