【问题标题】:How to include credentials when making CORS requests from Blazor?从 Blazor 发出 CORS 请求时如何包含凭据?
【发布时间】:2019-12-09 16:52:00
【问题描述】:

在 blazor 客户端应用程序上,jQuery ajax WithCredentials 或 JavaScript credentials: 'include' 的等价物是什么?

使用 Javascript 我可以说:

fetch('https://www.example.com/api/test', {
   credentials: 'include'
});

在发出请求时包括身份验证 cookie,服务器响应 200。我正在尝试使用 HttpClient 与 Blazor 编写相同的内容。

【问题讨论】:

标签: asp.net-core cors blazor


【解决方案1】:

在您的 Startup.Configure 方法中,您可以将 WebAssemblyHttpMessageHandler.DefaultCredentials 设置为出站 HTTP 请求的“凭据”选项的所需值,如下所示:

public void Configure(IComponentsApplicationBuilder app)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY")))
            {

                WebAssemblyHttpMessageHandler.DefaultCredentials = FetchCredentialsOption.Include;
            }

            app.AddComponent<App>("app");
        }

参考资料: https://github.com/aspnet/AspNetCore/blob/c39fbb8f12002f61df6c093b0c11e6bd585ee202/src/Components/Blazor/Blazor/src/Http/WebAssemblyHttpMessageHandler.cs

https://github.com/aspnet/AspNetCore/blob/5a70f5312fb57fc3788e5af56a99e7b43761e195/src/Components/Blazor/Blazor/src/Http/FetchCredentialsOption.cs

https://github.com/aspnet/AspNetCore/blob/d18a033b1ee6d923a72d440718c5d496b57c2ffc/src/Components/test/testassets/BasicTestApp/Startup.cs

希望这会有所帮助...

【讨论】:

  • 哇!只是工作!不太确定这是否是推荐的方式。我得检查一下?
  • 我也不确定...:)。但这正是 Blazor 团队使用的代码。
  • 这似乎在 Preview3 中不起作用,但对 Preview4 进行了修复:github.com/aspnet/AspNetCore/issues/17115
猜你喜欢
  • 2018-10-31
  • 2023-02-24
  • 1970-01-01
  • 2019-12-04
  • 2017-08-06
  • 2012-02-24
  • 2019-09-02
  • 2017-03-02
  • 2017-08-05
相关资源
最近更新 更多