【问题标题】:Enabling CORS using F# and ASP.NET使用 F# 和 ASP.NET 启用 CORS
【发布时间】:2018-09-12 12:30:04
【问题描述】:

如何使用 F# 和 ASP.NET 启用 CORS。我正在尝试通过 Google 对用户进行身份验证,但在浏览器控制台中出现以下错误:

请求中没有“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:8080” 使用权。如果不透明的响应满足您的需求,请设置请求的 模式为“no-cors”以获取禁用 CORS 的资源。

这里有一个可重现的例子https://github.com/sashang/safe-google-auth

它使用 F# 和 SAFE 框架,因此如果您想尝试重现此设置,则需要该设置。

您还需要使用 Google+ API (https://console.cloud.google.com/apis/library/plus.googleapis.com) 设置客户端 ID 和密码

完成后将其添加到您的环境中:

重击:

export GOOGLE_ID="client id"
export GOOGLE_SECRET="client secret"

Windows Powershell:

$Env:GOOLGE_ID="client id"
$Env:GOOGLE_SECRET="client secret"

要构建它,请克隆 repo 并运行

fake build --target run

然后在浏览器控制台窗口打开的情况下单击按钮Auth with Google 以查看错误。

【问题讨论】:

    标签: asp.net f# cors saturn-framework


    【解决方案1】:

    在您的 Server.fs 中,您不会调用您的 configure_cors 方法

    let app google_id google_secret = 
        application {
            url ("http://0.0.0.0:" + port.ToString() + "/")
            use_router webApp
            memory_cache
            use_static publicPath
            service_config configureSerialization
            use_gzip
            use_google_oauth google_id google_secret "/oauth_callback_google" []
            //use_cors "localhost:8080" configure_cors
            configure_cors // Add this line to your program
    
    }
    

    【讨论】:

    • 我以为 saturn 框架会通过 use_cors 调用它。请参阅上面注释 use_cors 的行。我确实在未注释的情况下尝试过。我真的必须明确地将其称为configure_cors吗?如果我尝试它不会编译,因为应用程序是计算表达式,需要一些特殊的东西,但我不知道是什么。
    猜你喜欢
    • 2019-11-18
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2014-05-11
    • 2021-03-08
    相关资源
    最近更新 更多