ramax495

ramax495

Member Since 6 years ago

Experience Points
0
follower
Lessons Completed
4
follow
Lessons Completed
99
stars
Best Reply Awards
6
repos

6 contributions in the last year

Pinned
⚡ Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure
⚡ Asp.NET Core 2.0 WebApi JWT Authentication with Identity & MySQL
⚡ Clean Architecture Solution Template for Angular 9 and .NET Core 3.1
Activity
Nov
25
1 week ago
Nov
23
1 week ago
Nov
1
1 month ago
started
started time in 1 month ago
Oct
17
1 month ago
Activity icon
issue

ramax495 issue comment jasontaylordev/CleanArchitecture

ramax495
ramax495

Question: Enabling CORS - How To Fix Cors Policy Issue?

Hello,

How do I enable CORS. I'm currently trying to connect my Angular to the backEnd but still hitting the CORS policy issue.

I currently have the below code under ConfigureService() method in the startup file.

services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200").Build(); }); });

And the below code under Configure() on same startup file.

app.UseCors("CorsPolicy");

Even after doing this, I still run into CORS policy issue.

Please, am I missing out something?

Appreciate any help.

ramax495
ramax495

@ngajugodwin I've found a short explanation: This issue is not with CORS, the https is causing this issue but thrown error is saying its with CORS. https://stackoverflow.com/a/60053610/10886266

Activity icon
issue

ramax495 issue comment jasontaylordev/CleanArchitecture

ramax495
ramax495

Question: Enabling CORS - How To Fix Cors Policy Issue?

Hello,

How do I enable CORS. I'm currently trying to connect my Angular to the backEnd but still hitting the CORS policy issue.

I currently have the below code under ConfigureService() method in the startup file.

services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200").Build(); }); });

And the below code under Configure() on same startup file.

app.UseCors("CorsPolicy");

Even after doing this, I still run into CORS policy issue.

Please, am I missing out something?

Appreciate any help.

ramax495
ramax495

@ngajugodwin Soryy, but I dont't know the reason. It's very strange but MS documentation allows calling UseHttpsRedirection before UseCors: https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0#cors-with-named-policy-and-middleware-1

There is related issue in dotnet repo: https://github.com/dotnet/AspNetCore.Docs/issues/19957

Activity icon
issue

ramax495 issue comment jasontaylordev/CleanArchitecture

ramax495
ramax495

Question: Enabling CORS - How To Fix Cors Policy Issue?

Hello,

How do I enable CORS. I'm currently trying to connect my Angular to the backEnd but still hitting the CORS policy issue.

I currently have the below code under ConfigureService() method in the startup file.

services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200").Build(); }); });

And the below code under Configure() on same startup file.

app.UseCors("CorsPolicy");

Even after doing this, I still run into CORS policy issue.

Please, am I missing out something?

Appreciate any help.

ramax495
ramax495

But i noticed that if i didnt put endpoints.MapControllers().RequireCors("CorsPolicy");

It's optional way.

In one case you add your configured named policy in app.UseCors("CorsPolicy"); and it applies to all endpoints by default.

In other case you add your named policy manually to each endpoint using .RequireCors("CorsPolicy"); and call app.UseCors(); without any arguments (it's more flexible way if you need).

In one more case you can just configure default policy using AddDefaultPolicy and also call app.UseCors(); without any arguments.

But all in all nothing of this works if you call UseHttpsRedirection before UseCors 😊

Activity icon
issue

ramax495 issue comment jasontaylordev/CleanArchitecture

ramax495
ramax495

Question: Enabling CORS - How To Fix Cors Policy Issue?

Hello,

How do I enable CORS. I'm currently trying to connect my Angular to the backEnd but still hitting the CORS policy issue.

I currently have the below code under ConfigureService() method in the startup file.

services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200").Build(); }); });

And the below code under Configure() on same startup file.

app.UseCors("CorsPolicy");

Even after doing this, I still run into CORS policy issue.

Please, am I missing out something?

Appreciate any help.

ramax495
ramax495

I see you edited your comment.

I think the key is app.UseCors(); app.UseHttpsRedirection(); 😊

Activity icon
issue

ramax495 issue comment jasontaylordev/CleanArchitecture

ramax495
ramax495

Question: Enabling CORS - How To Fix Cors Policy Issue?

Hello,

How do I enable CORS. I'm currently trying to connect my Angular to the backEnd but still hitting the CORS policy issue.

I currently have the below code under ConfigureService() method in the startup file.

services.AddCors(opt => { opt.AddPolicy("CorsPolicy", policy => { policy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200").Build(); }); });

And the below code under Configure() on same startup file.

app.UseCors("CorsPolicy");

Even after doing this, I still run into CORS policy issue.

Please, am I missing out something?

Appreciate any help.

ramax495
ramax495

Try these variants:

  1. Add named policy in ConfigureServices:
services.AddCors(options =>
{
    options.AddPolicy("CorsPolicy",
      builder =>
      {
          builder
            .AllowAnyMethod()
            .AllowAnyHeader()
            .SetIsOriginAllowed(origin => true) // = any origin
            .AllowCredentials();
      });
});

In Configure: app.UseCors(); and add RequireCors to endpoints:

endpoints.MapControllers().RequireCors("CorsPolicy");

or enable CORS with attributes in your controllers:

[EnableCors("CorsPolicy")]
  1. Add default policy in ConfigureServices:
services.AddCors(options =>
{
    options.AddDefaultPolicy(
        builder =>
        {
            builder
            .AllowAnyMethod()
            .AllowAnyHeader()
            .SetIsOriginAllowed(origin => true) // = any origin
            .AllowCredentials();
        });
});

In Configure app.UseCors();

Sep
16
2 months ago
Sep
9
2 months ago