4

I am working a asp .net core 6.0 web Api.(We are moving to asp .net core from 3.1 to 6.0)

I did enable cors in our asp .net core 6.0 project and works fine earlier (this morning ). But Now I faced cors error.

Error: Access to XMLHttpRequest at 'http://localhost:5000/api/user/authenticate' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

I revoked my git commits and checked. still It give CORS error

This is my program.cs

var app = builder.Build();

app.UseHttpsRedirection();
app.UseForwardedHeaders();
app.UseCors(builder => builder.WithOrigins("*")/*.AllowAnyOrigin()*/.AllowAnyHeader().AllowAnyMethod());
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

The same code worked fine earlier in asp .net core 6.0. Also Now In asp .netcore 3.1 project It is working fine.

Note: I searched about this error. And Tried same as on this answer How to enable cors in ASP.NET Core 6.0 Web API project?. But It did not work for me.

What is the mistake I did? Why this error occurs ( Today morning I did not face this error. But now facing this error. I revoked my new commits too. But CORS error)

Is there any other reasons to get this CORS error. I guess mistake is on my asp.net core 6.0 project, because asp .net core 3.1 project is working fine

5
  • did you fix this issue?
    – Thushara
    May 18, 2022 at 6:28
  • var builder = WebApplication.CreateBuilder(args); builder.Services.AddCors(options => { options.AddPolicy("ClientPermission", policy => { policy.AllowAnyHeader() .AllowAnyMethod() .SetIsOriginAllowed(_ => true) .AllowCredentials(); }); }); var app = builder.Build(); app.UseCors("ClientPermission"); // Configure the HTTP request pipeline if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
    – hanushi
    May 19, 2022 at 4:27
  • try with above code
    – hanushi
    May 19, 2022 at 4:30
  • Still does not work
    – rooter
    May 19, 2022 at 15:07
  • Did you find an answer ?
    – rak007
    Jun 17, 2022 at 20:59

3 Answers 3

4

app.UseCors should come after app.UseRouting();

according to the official documentation https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0

0

try this syntax

            builder.Services.AddCors(opt =>  opt.AddPolicy("CorsPolicy", c =>
            { 
                c.AllowAnyOrigin()
                   .AllowAnyHeader()
                   .AllowAnyMethod();
            }));

         .....

            app.UseCors("CorsPolicy") 
2
  • 1
    I tried with this. But not working for me.
    – hanushi
    Jan 28, 2022 at 3:04
  • I faced a similar problem - have you checked whether your browser needs a Cors Unblock extension? I was facing Cors related issues until I installed and activated the Edge extension Cors Unblock.
    – user5830202
    Jun 11, 2022 at 8:15
0

You can edit your web.config. It worked for me with .NET Core 5.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <modules runAllManagedModulesForAllRequests="false">
                <remove name="WebDAVModule" />
            </modules>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"   />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\project.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>
<!--ProjectGuid: 043EE210-85D7-4EA4-A1CB-4FA6BCD39F0A-->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.