Fetch API 调用导致新的 Asp.net 会话
Fetch API call causes new Asp.net session
我正在删除我的一个 asp.net mvc 项目中的 jQuery 以支持使用纯 vanilla JS。现在我已经用 Fetch API 调用替换了 $.ajax
POST 调用,每个调用都会在服务器上触发一个新会话。
在过去的几天里,这一直让我感到困惑,我已经将其缩小到具体的从使用 jQuery Ajax 到 Fetch API 的转换。我的新 Fetch API 调用在其他方面工作得很好,仍然执行所需的服务器端工作。一旦他们 return.
就会触发一个新的服务器会话
显然,这是一个主要问题,因为我的用户会话数据不断被重置。关于为什么会发生这种情况的任何想法?或者任何人都知道任何解决方法,而不必恢复使用 jQuery?
我之前基于 'jQuery' 的 POST 通话:
Post(route, data) {
$.ajax({
type: 'POST',
url: route,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8"
}).done((result, statusText, jqXHR) => {
return result;
});
}
我的新 'Fetch API' 通话:
async Post(route, data) {
let response = await fetch(route, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
let result = await response.json();
return result;
}
在我的 Global.asax.cs
:
protected void Session_Start(object o, EventArgs e) {
Debug.WriteLine("Session_Start");
HttpContext.Current.Session.Add("__MySessionData", new MySessionDataClass());
}
正如我上面提到的,除了重置我的会话外,Fetch API 调用工作得很好,我从 Debug.WriteLine
调用中知道这一点。 jQuery Ajax 调用也可以正常工作,并且不会触发新会话,但是我正在尝试删除对 jQuery.
的依赖
想法?
在@gunr2171 的帮助下解决了这个问题,所以自我回答以防其他人遇到类似问题。
事实证明,我的新 Fetch API 调用没有随请求发送当前 Asp.net 会话 cookie,因此服务器会启动一个新会话,认为会话尚不存在。调整 Fetch API 调用以在选项中包含 credentials: 'include'
允许它发送当前会话 cookie,并且服务器将不再在每次调用后创建一个新的。
作为参考,我的新 Fetch API 调用现在看起来像:
async Post(route, data) {
let response = await fetch(route, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(data)
});
let result = await response.json();
return result;
}
您没有在自定义请求中传递 ASP.NET_SessionId
cookie。
您正在使用 fetch
. By default 它使用 omit
作为 credentials
。这意味着,如 MDN 页面上所述:
By default, fetch
won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
JQuery 确实 发送 cookie,但仅发送同一域中的 cookie。
AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script.
Source
要解决此问题,您需要告诉 fetch
发送 cookie。来自 :
fetch('/something', { credentials: 'same-origin' }) // or 'include'
我正在删除我的一个 asp.net mvc 项目中的 jQuery 以支持使用纯 vanilla JS。现在我已经用 Fetch API 调用替换了 $.ajax
POST 调用,每个调用都会在服务器上触发一个新会话。
在过去的几天里,这一直让我感到困惑,我已经将其缩小到具体的从使用 jQuery Ajax 到 Fetch API 的转换。我的新 Fetch API 调用在其他方面工作得很好,仍然执行所需的服务器端工作。一旦他们 return.
就会触发一个新的服务器会话显然,这是一个主要问题,因为我的用户会话数据不断被重置。关于为什么会发生这种情况的任何想法?或者任何人都知道任何解决方法,而不必恢复使用 jQuery?
我之前基于 'jQuery' 的 POST 通话:
Post(route, data) {
$.ajax({
type: 'POST',
url: route,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8"
}).done((result, statusText, jqXHR) => {
return result;
});
}
我的新 'Fetch API' 通话:
async Post(route, data) {
let response = await fetch(route, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
},
body: JSON.stringify(data)
});
let result = await response.json();
return result;
}
在我的 Global.asax.cs
:
protected void Session_Start(object o, EventArgs e) {
Debug.WriteLine("Session_Start");
HttpContext.Current.Session.Add("__MySessionData", new MySessionDataClass());
}
正如我上面提到的,除了重置我的会话外,Fetch API 调用工作得很好,我从 Debug.WriteLine
调用中知道这一点。 jQuery Ajax 调用也可以正常工作,并且不会触发新会话,但是我正在尝试删除对 jQuery.
想法?
在@gunr2171 的帮助下解决了这个问题,所以自我回答以防其他人遇到类似问题。
事实证明,我的新 Fetch API 调用没有随请求发送当前 Asp.net 会话 cookie,因此服务器会启动一个新会话,认为会话尚不存在。调整 Fetch API 调用以在选项中包含 credentials: 'include'
允许它发送当前会话 cookie,并且服务器将不再在每次调用后创建一个新的。
作为参考,我的新 Fetch API 调用现在看起来像:
async Post(route, data) {
let response = await fetch(route, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(data)
});
let result = await response.json();
return result;
}
您没有在自定义请求中传递 ASP.NET_SessionId
cookie。
您正在使用 fetch
. By default 它使用 omit
作为 credentials
。这意味着,如 MDN 页面上所述:
By default,
fetch
won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).
JQuery 确实 发送 cookie,但仅发送同一域中的 cookie。
AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script.
Source
要解决此问题,您需要告诉 fetch
发送 cookie。来自
fetch('/something', { credentials: 'same-origin' }) // or 'include'