Wednesday, May 11, 2022

Enable Cross-Origin Requests (CORS) in ASP.NET Core

Hello, All if you are starting working on the .net core I am 100% you are facing the below error at your local.


The solution is very simple you have to add Code in the StartUp or Program file.

var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

builder.Services.AddCors(options =>

{

    options.AddPolicy(name: MyAllowSpecificOrigins,

                      policy =>

                      {                 

                          policy

                          .AllowCredentials()

                          .SetIsOriginAllowed((host) => true)

                          .AllowAnyHeader()

                          .AllowAnyMethod();

                      });

});

app.UseCors(MyAllowSpecificOrigins);


We have the above code for UI project and Service(Web api) projects.


Thank you !!!

No comments:

Post a Comment