Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS Configuration Not Working #512

Open
williamoanta opened this issue May 1, 2022 · 1 comment
Open

CORS Configuration Not Working #512

williamoanta opened this issue May 1, 2022 · 1 comment

Comments

@williamoanta
Copy link

williamoanta commented May 1, 2022

I am trying to configure CORS for my backend, but I still get the error:

Access to XMLHttpRequest at 'X' from origin 'Y' 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.

Below is my code which has the proper configuration - as per the documentation and samples:

let configureCors (builder : CorsPolicyBuilder) =
    builder.AllowAnyOrigin()
           .AllowAnyMethod()
           .AllowAnyHeader()
           |> ignore

let configureApp (app : IApplicationBuilder) =
    let env = app.ApplicationServices.GetService<IHostingEnvironment>()
    
    // Development State can be set via ASPNETCORE_ENVIRONMENT Environment Variable. Checking it here.
    let env_state = env.IsDevelopment()
    logger.debug("configureApp()", [env_state], "Environment State, Development:")
    
    (match env.IsDevelopment() with
    | true  -> app.UseCors(configureCors).UseDeveloperExceptionPage()
    | false -> app.UseCors(configureCors).UseGiraffeErrorHandler errorHandler)
        .UseHttpsRedirection()
        .UseStaticFiles()
        .UseGiraffe(webApp)

let configureServices (services : IServiceCollection) =
    services.AddCors()    |> ignore
    services.AddGiraffe() |> ignore

let configureLogging (builder : ILoggingBuilder) =
    builder.AddFilter(fun l -> l.Equals LogLevel.Error)
           .AddConsole()
           .AddDebug() |> ignore

[<EntryPoint>]
let main _ =
    let contentRoot = Directory.GetCurrentDirectory()
    let webRoot     = Path.Combine(contentRoot, "WebRoot")
    WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(contentRoot)
        .UseIISIntegration()
        .UseWebRoot(webRoot)        
        .UseUrls("http://0.0.0.0:8081")
        .Configure(Action<IApplicationBuilder> configureApp)
        .ConfigureServices(configureServices)
        .ConfigureLogging(configureLogging)
        .Build()
        .Run()
    0

Also the package is imported like this:
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />

Any ideas what I am doing wrong?

Thank you!

@kphuanghk
Copy link

I copied your code with insignificant and small change to make the code run in my env, using dotnet5.

The CORS just works. You may review if your client makes call to the right endpoint.

let configureApp (app : IApplicationBuilder) =
    // Using IHostEnvironment
    let env = app.ApplicationServices.GetService<IHostEnvironment>()
    
    // Development State can be set via ASPNETCORE_ENVIRONMENT Environment Variable. Checking it here.
    let env_state = env.IsDevelopment()
    // logger.debug("configureApp()", [env_state], "Environment State, Development:")

In my case I do not need to refer to this

<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants