ASP Net Core Web API 无法识别会话数据
ASP Net Core Web API doesn't recognize session data
我目前正在从事一个项目,该项目由一个简单的前端(作为一个 HTML 页面)和一个 ASP.NET 核心后端(Web-API)组成。我目前正在处理 register/login 流程。登录方法由 GET 请求触发,注册方法由 POST 请求触发。 register 方法为会话 cookie 设置了四个值,我们称它们为 valA、valB、valC 和 valD。之后通过login方法获取对应的值。到目前为止,我已经尝试了以下解决方案来实现这一点:
- 修改 CookiePolicyOptions 以绕过 ConsentCheck 并将 SameSitePolicy 设置为“None”
- 已应用 CookiePolicy
- 已检查用户控制器是否有错误 - 到目前为止没有任何错误
更新
有几个因素导致了前面提到的情况。首先,就像建议的标记答案一样,cookie 被发送到客户端,但没有返回。这个问题有几个原因。第一个问题是 HTML 的 javascript 代码正在发送登录请求。这可以通过在客户端添加以下代码并将以下函数设置为登录按钮的 OnClickHandler 来解决:
function PostLoginRequest()
{
fetch(someUrl,{
/*
* The credentials header enables the sending
* process of the response cookie, while the
* sameSite header is used to enable the cookie
* sent accross the original domain (in this case, the HTML file)
*/
"credentials":"include",
"sameSite":"None"
});
}
此外,您还必须调整Startup.cs文件的ConfigureServices方法的内容。在这种情况下,您将添加这些新行:
// Configure a cookie policy which will be enforced by the Web API
services.Configure<CookiePolicyOptions>(options=>{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.Secure = CookieSecurePolicy.Always;
});
// Add a storage for the session cookies, in this case a DMC
services.AddDistributedMemoryCache();
// Configure the session cookie
services.AddSession(options=>{
// Set the name of the session cookie
options.Cookie.Name = ".App.Session";
// How long should the cookie be stored? (in this case 1 day from now)
options.IdleTimeOut = TimeSpan.FromDays(1);
// Can we bypass the consent check? (in this case : yes)
options.Cookie.IsEssential = true;
// Prevent the cookie to be accesible via Javascript
options.Cookie.HttpOnly = true;
// Allow the cookie to be sent to other domains
options.Cookie.SameSite = SameSiteMode.None;
// Sets the path of the cookie. This means on which segment of
// the domain it will be accessible. In this case, the whole domain
// is covered by the cookie
options.Cookie.Path = "/";
});
最后但同样重要的是,Startup.cs 应该包含函数调用 app.UseSession() 和app.UseCookiePolicy()。虽然第一个调用使 ASP.NET 核心服务器能够发送会话 cookie,但如果其中存储了某些值,则第二个调用将应用我们之前定义的 cookie 策略。到目前为止,这应该是一切。抱歉更新时间过长,但希望我的解决方案描述能帮助到和我遇到同样问题的其他人。
您的 HTML 页面需要重新发送 cookie,如果它不发送 ASP.Net 网站 api 无法接收任何值。
这里您的 Asp.Net 网络 api 只是一个 api,它无法控制您的 html 页面。
您如何从 HTML 页面进行 api 调用?
我目前正在从事一个项目,该项目由一个简单的前端(作为一个 HTML 页面)和一个 ASP.NET 核心后端(Web-API)组成。我目前正在处理 register/login 流程。登录方法由 GET 请求触发,注册方法由 POST 请求触发。 register 方法为会话 cookie 设置了四个值,我们称它们为 valA、valB、valC 和 valD。之后通过login方法获取对应的值。到目前为止,我已经尝试了以下解决方案来实现这一点:
- 修改 CookiePolicyOptions 以绕过 ConsentCheck 并将 SameSitePolicy 设置为“None”
- 已应用 CookiePolicy
- 已检查用户控制器是否有错误 - 到目前为止没有任何错误
更新
有几个因素导致了前面提到的情况。首先,就像建议的标记答案一样,cookie 被发送到客户端,但没有返回。这个问题有几个原因。第一个问题是 HTML 的 javascript 代码正在发送登录请求。这可以通过在客户端添加以下代码并将以下函数设置为登录按钮的 OnClickHandler 来解决:
function PostLoginRequest()
{
fetch(someUrl,{
/*
* The credentials header enables the sending
* process of the response cookie, while the
* sameSite header is used to enable the cookie
* sent accross the original domain (in this case, the HTML file)
*/
"credentials":"include",
"sameSite":"None"
});
}
此外,您还必须调整Startup.cs文件的ConfigureServices方法的内容。在这种情况下,您将添加这些新行:
// Configure a cookie policy which will be enforced by the Web API
services.Configure<CookiePolicyOptions>(options=>{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.Secure = CookieSecurePolicy.Always;
});
// Add a storage for the session cookies, in this case a DMC
services.AddDistributedMemoryCache();
// Configure the session cookie
services.AddSession(options=>{
// Set the name of the session cookie
options.Cookie.Name = ".App.Session";
// How long should the cookie be stored? (in this case 1 day from now)
options.IdleTimeOut = TimeSpan.FromDays(1);
// Can we bypass the consent check? (in this case : yes)
options.Cookie.IsEssential = true;
// Prevent the cookie to be accesible via Javascript
options.Cookie.HttpOnly = true;
// Allow the cookie to be sent to other domains
options.Cookie.SameSite = SameSiteMode.None;
// Sets the path of the cookie. This means on which segment of
// the domain it will be accessible. In this case, the whole domain
// is covered by the cookie
options.Cookie.Path = "/";
});
最后但同样重要的是,Startup.cs 应该包含函数调用 app.UseSession() 和app.UseCookiePolicy()。虽然第一个调用使 ASP.NET 核心服务器能够发送会话 cookie,但如果其中存储了某些值,则第二个调用将应用我们之前定义的 cookie 策略。到目前为止,这应该是一切。抱歉更新时间过长,但希望我的解决方案描述能帮助到和我遇到同样问题的其他人。
您的 HTML 页面需要重新发送 cookie,如果它不发送 ASP.Net 网站 api 无法接收任何值。 这里您的 Asp.Net 网络 api 只是一个 api,它无法控制您的 html 页面。 您如何从 HTML 页面进行 api 调用?