我是 ASP.NET CORE 的新手,所以请理解我。
我目前正在为 .Net Core 中的应用程序开发登录和注销流程。
我的问题是:
- 注销后, session 不会被放弃或删除。
- 注销后,如果我点击浏览器的后退按钮,它将重定向到您触发注销按钮的页面。
这些是我的想法,但我不知道如何实现。
- 如果用户退出, session 将被删除并放弃。
- 用户单击“注销”,然后单击浏览器的后退按钮后,我想显示
确认表单重新提交
并将其重定向到“登录”,以便他可以登录其帐户。
这是我的代码:
LoginController.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);
}
}
HomeController.cs
public IActionResult Logout()
{
AuthenticationHttpContextExtensions.SignOutAsync(HttpContext
, CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Login", "Login");
}
Startup.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>
关于c# - 如何从我的 ASP.NET Core 应用程序中永久注销用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68294996/