Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of CORS in .net core 2.2?

I've updated my project to .net core 2.2 and it seems like CORS is making problems that weren't there in 2.1.

I'm running my app on this URL: http://*:5300

I've added this code in the Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddCors(options =>
                     options.AddPolicy("MyPolicy", builder =>
                     {
                         builder.AllowAnyOrigin()
                                .AllowAnyMethod()
                                .AllowCredentials()
                                .AllowAnyHeader();
                     }));

    services.AddMvc();

    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...

    app.UseCors(builder =>
    {
        builder.AllowAnyOrigin()
               .AllowAnyMethod()
               .AllowCredentials()
               .AllowAnyHeader();
    });

    app.UseAuthentication();
    app.UseMvc();
}

This didn't work, so I've added on top of it the [EnableCors] attribute on my `BaseController" class:

[EnableCors]
[Authorize]
[Produces("application/json")]
[Route("api/[controller]")]
public class BaseController : Controller
{

}

But I'm still getting this CORS error:

Access to XMLHttpRequest at 'http://192.168.15.63:5301/api/permissions/UI' from origin 'http://192.168.15.63:5302' 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.

What else can I do in order to completely remove CORS?

like image 438
Liran Friedman Avatar asked Dec 26 '18 12:12

Liran Friedman


People also ask

How do I disable CORS in net core?

Based on this information, you can fix your issue in one of two ways: Remove the policyName parameter from your call to UseCors . Remove the UseCors call itself.

What is CORS .NET core?

CORS means cross-origin resource sharing. You'll see more in just a minute, but in a nutshell, CORS is a mechanism—an HTTP protocol, to be exact—that allows web applications to access resources hosted on different domains (or origins.)

How to enable Cors in ASP NET Core?

An ASP.NET Core website can enable CORS quite easily. You just need to follow below steps: 1. Install the Microsoft.AspNetCore.Cors Nuget package. 2.

How to enable Cors with NuGet?

Install Nuget package: Microsoft.AspNetCore.Cors. For the installation we have 2 way to do it. Using application Nuget search. After nuget package is installed you will be able to see it in your application package library. Configure CORS startup class inside the ConfigureService method. Enable CORS using middleware in the Configure method.

How do I enable Cors in middleware?

Enable CORS. There are three ways to enable CORS: In middleware using a named policy or default policy. Using endpoint routing. With the attribute. Using the attribute with a named policy provides the finest control in limiting endpoints that support CORS.

How do I fix a failed Cors request?

Select the GetValues2 [DisableCors] button to trigger a failed CORS request. As mentioned in the document, the response returns 200 success, but the CORS request is not made. Select the Console tab to see the CORS error. Depending on the browser, an error similar to the following is displayed:


1 Answers

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'.

You cannot use both AllowAnyOrigin and AllowCredentials when using ASP.NET Core to respond to a CORS request.

Access to XMLHttpRequest at 'http://192.168.15.63:5301/api/permissions/UI' from origin 'http://192.168.15.63:5302' has been blocked by CORS policy

This message shows that your server is listening on http://192.168.15.63:5301, but your client is making the request from http://192.168.15.63:5302. Since the port is different, these are different origins and therefore CORS protection is used.

To allow the request to succeed, update your ASP.NET CORS configuration code to something like the following:

builder.WithOrigins("http://192.168.15.63:5302")
    .AllowAnyMethod()
    .AllowCredentials()
    .AllowAnyHeader();

This configures the origin of the client as being supported by CORS - you could, of course, add this as a configuration option to the application itself (using e.g. appsettings.json), if needed.


Aside:

As you've called AddCors and configured a named policy, there is no reason to configure the same policy in the call to UseCors - you can simply pass in the name of the policy you configured earlier with AddCors:

app.UseCors("MyPolicy");
like image 111
Kirk Larkin Avatar answered Oct 20 '22 00:10

Kirk Larkin