asp.net-core - 从 Blazor 发出 CORS 请求时如何包含凭据?
问题描述
在 Blazor 客户端应用程序上,jQuery ajaxWithCredentials
或 JavaScript的等价物是什么credentials: 'include'
?
使用 Javascript 我可以说:
fetch('https://www.example.com/api/test', {
credentials: 'include'
});
其中包括 auth cookie,同时发出请求,服务器响应 200。我正在尝试使用 Blazor 编写相同的HttpClient
.
解决方案
在您的 Startup.Configure 方法中,您可以将 WebAssemblyHttpMessageHandler.DefaultCredentials 设置为出站 HTTP 请求的“凭据”选项的所需值,如下所示:
public void Configure(IComponentsApplicationBuilder app)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("WEBASSEMBLY")))
{
WebAssemblyHttpMessageHandler.DefaultCredentials = FetchCredentialsOption.Include;
}
app.AddComponent<App>("app");
}
希望这可以帮助...
推荐阅读
- jquery - 输入中只允许数字和“.”(点)
- angular - Angular Reactive Forms,将数据对象与 FormArray 的每个对象绑定的方法
- c# - 如何打开 Thumbscache.db
- sql-server - 在使用 .Net 创建 SQL DDL 语句时,我们可以忽略大小或以正确的方式放置它吗?
- react-native - 使用 react-native 和 cljs-ajax 时出现错误“无法解析模块 xmlhttprequest”
- node.js - TypeORM RepositoryNotFoundError:找不到 %% 的存储库。如何使用模式注册存储库
- python - 从分组的熊猫数据框中绘制堆积图
- php - 从特定的单个页面中删除 .php 扩展名
- javascript - 从具有相同 id 的 div 复制文本以输入 onclick
- webstorm - WebStorm:如何禁用 TypeScript 的结束语句警报?