Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 2.2 CORS Not Allowing Requests

I have checked several other threads on this and still can't manage to figure this out. I'm wanting to allow any origin, header, method, etc. to access my .NET Core 2.2 API.

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

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, IDistributedCache cache)
    {
        app.UseCors(builder => builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials());

I made sure the CORS methods were called first within ConfigureServices and Configure. I'm testing this locally and from the server and get this same error on both in Chrome.

Access to XMLHttpRequest at 'https://xxxxx.azurewebsites.net/api/Employees/getCurrentEmployee' from origin 'https://xxxxxxxxxx.azurewebsites.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
like image 415
jrichmond4 Avatar asked Mar 05 '23 13:03

jrichmond4


1 Answers

in ConfigureServices method

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

in configure method

app.UseCors("CorsPolicy");
like image 150
prisar Avatar answered Mar 12 '23 09:03

prisar