c# - 即使我重新启动程序,ASP.NET CORE 3.1 Cookie 也始终有效
问题描述
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationSchema, principal);
//...
//Then
HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationSchema);
return Redirect ……
我看到它让cookie立即过期。在响应标头中使用 set-cookie : XXXX 1970 XXXX。但是当我通过 chrome f12 应用程序 cookie 添加 cookie 时,我发现它仍然可以工作。即使我重新启动程序,
this.context.HttpContext.User.Identity.IsAuthenticated
是真的。饼干仍然有效...
解决方案
如果 cookie 在浏览器会话中持续存在。只有当用户明确同意并在登录时选中“记住我”复选框或类似机制时,才能启用这种持久性。就像这个代码示例:
// using Microsoft.AspNetCore.Authentication;
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
new AuthenticationProperties
{
IsPersistent = true
});
此代码片段创建了一个身份和相应的 cookie,它们在浏览器关闭后仍然存在。任何先前配置的滑动到期设置都将受到尊重。如果cookie在浏览器关闭时过期,浏览器会在重启后清除cookie。https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-5.0
推荐阅读
- python - 如何使用包含每个交换机的字典的文件通过 SSH 连接到多个交换机?
- python - 在 python 中从我的数据文件中保存/读取错误的数据
- cuda - 如何将字符指针从设备复制到主机
- java - 有没有办法将电话号码标准化为 1-222-444-5555(根据北美标准)?
- angular - 添加文件夹后重新渲染Angular
- android - Android Studio 3.5.2 - 错误:您当前选择的变体 (app-release-unsigned.apk) 的 apk 未签名
- arrays - 如何从任何结构类型派生结构列表 - 从 interface{} 到可变长度切片 []interface{}
- r - 按列的总和以升序对数据进行排序
- azure - 应用服务上的 httpcontextaccessor 空引用但不调试
- r - 如何使用 R Programming 在 Sagemaker 中加载现有的 S3 存储桶?