我正在使用联合将包含用户名和密码的登录表单数据发布到端点。我收到了一个成功的响应,其中包含了预期的凭据(作为会话cookie),在Unity中测试时,随后的通信就成功地完成了。
当我以WebGL的形式部署和构建项目时,我不会从我的日志请求中接收到“Set-Cookie”头文件。这将导致所有后续通信都在未经授权的401.中失败。
'Set-Cookie'.
不同的服务器。
我想有安全措施(CORS?)在将响应返回给我的程序之前删除这些凭据。“允许凭据”和“原产地响应标题”显示正确。“‘Set Cookie”响应头格式是:
Set-Cookie: SESSION=tvohm-example-session; Path=/tvohm-example-path/; Secure; HttpOnly; SameSite=Lax缩小:
IEnumerator LogInCoroutine()
{
using var request = new UnityWebRequest("https://tvohm-example-url.com/login")
{
method = UnityWebRequest.kHttpVerbPOST,
uploadHandler = new UploadHandlerRaw(UnityWebRequest.SerializeSimpleForm(new Dictionary<string, string>()
{
{ "username", "tvohm" },
{ "password", "ilovestackoverflow" }
}))
};
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log(request.GetResponseHeader("Set-Cookie"));
// Editor returns expected session cookie
// WebGl returns null
}
}援引:
StartCoroutine(LogInCoroutine());发布于 2022-07-20 06:57:49
您不能在javascript中读取或写入"Set- Cookie "头,因为浏览器将其隐藏在您面前,因为是一个禁止的标题。 https://fetch.spec.whatwg.org/#forbidden-header-name浏览器将为您处理Cookie(因此从响应标头中裁剪该标题,以便您无法访问它),并在请求中自动添加Cookie标头,这样就不能泄露存储在"Cookie“头部中的关键凭据。
如果希望通过浏览器将Cookie附加到请求头,则必须通过添加
{credentials : „include“} 到javascript fetch函数。因为统一现在将c# UnityWebRequest转换为javascript (url,UnityWebRequest)函数。在过去,UnityWebRequest被翻译成XMLHttpRequest。您要做的是在为webGL构建统一之后,重写Javascript或HTML中的fetch()函数
<Skript>
fetch = function( url,data) {
console.log("url received: " + url);
if (url.indexOf('https://www.replacethatwithyourserveraddress.com/') === 0 || url.indexOf('http://localhost:4000') === 0 || url.indexOf('http://127.0.0.1:4000') === 0) {
data = {...data, ...{credentials : "include"}};
console.log("withCredentials set to true " + JSON.stringify(data) + " url: " + url );
} else {
console.log("withCredentials NOT SET for URL: " + url);
}
return originalfetch(url,data);
};
</Skript>在这里阅读更多有关这方面的信息:
https://braveyourself.de/cors-xmlhttprequest-unity-webgl-rest-netzworking-build/
https://stackoverflow.com/questions/72404284
复制相似问题