Skip to content

Commit

Permalink
设置 Cookie 的SameSite 为Lax
Browse files Browse the repository at this point in the history
  • Loading branch information
liukuo362573 committed Jan 11, 2022
1 parent 21b6d9b commit 4ef52f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
15 changes: 10 additions & 5 deletions YiSha.Web/YiSha.Admin.Web/Controllers/HomeController.cs
Expand Up @@ -67,13 +67,13 @@ public IActionResult Login()
return View();
}

[HttpGet]
public async Task<IActionResult> LoginOff()
[HttpPost]
public async Task<IActionResult> LoginOffJson()
{
#region 退出系统
OperatorInfo user = await Operator.Instance.Current();
if (user != null)
{
#region 退出系统
// 如果不允许同一个用户多次登录,当用户登出的时候,就不在线了
if (!GlobalContext.SystemConfig.LoginMultiple)
{
Expand All @@ -95,9 +95,14 @@ public async Task<IActionResult> LoginOff()

Operator.Instance.RemoveCurrent();
new CookieHelper().RemoveCookie("RememberMe");

return Json(new TData { Tag = 1 });
#endregion
}
else
{
throw new Exception("非法请求");
}
#endregion
return View(nameof(Login));
}

[HttpGet]
Expand Down
17 changes: 16 additions & 1 deletion YiSha.Web/YiSha.Admin.Web/Views/Home/Index.cshtml
Expand Up @@ -129,7 +129,7 @@
</li>
<li class="divider"></li>
<li>
<a href="@Url.Content("~/Home/LoginOff")">
<a href="#" onclick="loginOff()">
<i class="fa fa-sign-out"></i> 退出登录
</a>
</li>
Expand Down Expand Up @@ -203,4 +203,19 @@
maxmin: false
});
}
function loginOff() {
ys.ajax({
url: '@Url.Content("~/Home/LoginOffJson")',
type: "post",
success: function (obj) {
if (obj.Tag == 1) {
location.href = '@Url.Content("~/Home/Index")';
}
else {
ys.msgError(obj.Message);
}
}
});
}
</script>
2 changes: 2 additions & 0 deletions YiSha.Web/YiSha.Web.Code/State/CookieHelper.cs
Expand Up @@ -24,6 +24,7 @@ public void WriteCookie(string sName, string sValue, bool httpOnly = true)
CookieOptions option = new CookieOptions();
option.Expires = DateTime.Now.AddDays(30);
option.HttpOnly = httpOnly;
option.SameSite = SameSiteMode.Lax;
hca?.HttpContext?.Response.Cookies.Append(sName, sValue, option);
}

Expand All @@ -40,6 +41,7 @@ public void WriteCookie(string sName, string sValue, int expires, bool httpOnly
CookieOptions option = new CookieOptions();
option.Expires = DateTime.Now.AddMinutes(expires);
option.HttpOnly = httpOnly;
option.SameSite = SameSiteMode.Lax;
hca?.HttpContext?.Response.Cookies.Append(sName, sValue, option);
}

Expand Down

0 comments on commit 4ef52f5

Please sign in to comment.