c# - 如何从我的 asp.net 核心应用程序中永久注销用户?
问题描述
我是 ASP.NET CORE 的新手,所以请理解我。
我目前正在.Net Core 中为我的应用程序开发登录和注销流程。
我的问题是:
- 注销后,会话不会被放弃也不会被删除。
- 注销后,如果我点击浏览器的后退按钮,它将被重定向到您触发注销按钮的页面。
这些是我的想法,但我不知道如何去做。
- 如果用户注销,会话将被删除并放弃。
- 用户单击注销然后单击浏览器的后退按钮后,我想显示
Confirm Form Resubmission
并将他重定向到登录,他可以在其中登录他们的帐户。
这是我的代码:
登录控制器.cs
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login(LoginModel userLogin)
{
ILogicInterface<UserInput, SystemResult> dbLogic = new LoginLogic();
UserInput userInput = new UserInput();
userInput[typeof(LoginModel).FullName] = userLogin;
SystemResult systemResult = dbLogic.DoProcess(userInput);
bool userExist = systemResult.ResultCode == SystemResult.RESULT_CODE_SUCCESS;
if (userExist)
{
LoginInfomation loginInfomation = systemResult[typeof(LoginInfomation).FullName] as LoginInfomation;
HttpContext.Session.SetString("userInfo", JsonConvert.SerializeObject(loginInfomation));
Claim[] claims = new[] { new Claim(ClaimTypes.Name, loginInfomation.E_mail), new Claim(ClaimTypes.Role, AccountInformation.GetRole(loginInfomation.AccountInfo.roleID)) };
ClaimsIdentity identity = new(claims, CookieAuthenticationDefaults.AuthenticationScheme);
AuthenticationHttpContextExtensions.SignInAsync(HttpContext, new ClaimsPrincipal(identity));
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError(string.Empty, "The specified email or password is incorrect.");
return View(userLogin);
}
}
家庭控制器.cs
public IActionResult Logout()
{
AuthenticationHttpContextExtensions.SignOutAsync(HttpContext, CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Login", "Login");
}
启动.cs
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.IsEssential = true;
});
services.ConfigureApplicationCookie(options => {
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.LoginPath = new PathString("/Login/Login");
});
有人可以帮助我实现上面列出的想法。谢谢和问候,
解决方案
尝试在 web.config 中添加 customHeaders:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0" />
<add name="Pragma" value="no-cache" />
<add name="Expires" value="0" />
</customHeaders>
</httpProtocol>
</system.webServer>
推荐阅读
- yii2 - Yii2 会话在每次页面刷新时重新加载
- ruby-on-rails - 如何为业力导入文件-rails中的javascript单元测试?
- python - 使用字典对具有特定结构的熊猫数据帧进行空间插值
- algorithm - 将均匀分布转换为肥尾分布
- javascript - AJAX 数据未发布以在 Django 中查看
- ionic-framework - Change of language doesn't work instantly in ionic 3
- csv - 使用批处理文件有条件地替换 csv 中的值
- haskell - 可变参数列表附加函数,用于从 haskell 中的列表创建列表列表
- python - 如何在 Colab 中使用 upload() 命令而无需手动输入文件位置?
- java - 集合上包含(对象)的不太可能的参数类型字符串