【发布时间】:2019-06-02 07:24:09
【问题描述】:
我想通过 CORS 将我的前端网站连接到我的后端 API,该 API 使用简单的身份验证(名称和密码)来授权用户。为此,我使用 Javascripts Fetch API。即使我使用credentials = "initial",每个 fetch 调用的会话 ID 似乎也会发生变化:
function onClick() {
const url = 'https://localhost:44394/api/account/Login/Standard/P@$$w0rd';
fetch(url, {
method: 'POST',
credentials: 'include',
mode: 'cors'
}).then(function (response) {
return response.text().then(function (text) {
document.getElementById("at").innerHTML = text;
});
});
}
我在AspNetCore-Startup.cs 的Configure() 中添加了以下内容:
app.UseCors(builder=>builder.AllowAnyOrigin().AllowCredentials().AllowAnyMethod().AllowAnyHeader());
还有我的AspNetCore-Startup.csConfigureServices():
services.AddCors();
我无法使用onClick()-函数登录。当我使用 credentials: "same-origin" 而不是 include 时,我可以登录,但随后的 API 调用会更改调用的会话 ID。
如何防止 Fetch 调用更改会话 ID?
编辑:
在我的帐户控制器中,我通过HttpContext.Session 访问当前会话 ID。也许这类似于服务器会话而不是请求者会话?
【问题讨论】:
标签: javascript c# asp.net-core cors fetch