You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
The text was updated successfully, but these errors were encountered:
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.
letconfigureApp(app :IApplicationBuilder)=// Using IHostEnvironmentletenv= app.ApplicationServices.GetService<IHostEnvironment>()// Development State can be set via ASPNETCORE_ENVIRONMENT Environment Variable. Checking it here.letenv_state= env.IsDevelopment()// logger.debug("configureApp()", [env_state], "Environment State, Development:")
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:
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!
The text was updated successfully, but these errors were encountered: