【发布时间】:2020-06-18 00:46:30
【问题描述】:
当我的浏览器在我的 ASP.NET Core MVC 项目中关闭时,我需要删除 cookie 这包括我用来创建 cookie 的代码。
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Name,user.email),
new Claim(ClaimTypes.Role,user.roles)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var princilple = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, princilple);
if (User.IsInRole("Admin"))
{
return RedirectToAction("Dashboard", "Home", new { user.id });
}
else
{
return RedirectToAction("Details", "Home", new { user.id });
}
}
else
{
TempData["Message"] = "Invalid Username/Password";
return RedirectToAction("Index", "Home");
}
}
else
{
return RedirectToAction("Error", "Home");
}
【问题讨论】:
-
你可以在 unload 事件之前使用 js 和 window 来移除它,但是如果你只是想让 cookie 在浏览期间持续存在,为什么不使用 session 来代替呢?
-
如果您只想要 Session 级别的授权,为什么还要使用 cookie?
标签: javascript c# jquery cookies asp.net-core-mvc