如何从我的 asp.net 核心应用程序中永久注销用户?
How to permanently logout the user from my asp.net core application?
我是ASP.NET CORE的新人,所以请理解我。
我目前正在为 .Net Core 中的应用程序开发登录和注销流程。
我的问题是:
- 注销后,会话不会被放弃或删除。
- 注销后,如果我点击浏览器的后退按钮,它将被重定向到您触发注销按钮的页面。
这些是我的想法,但我不知道怎么做。
- 如果用户注销,会话将被删除并放弃。
- 用户点击注销然后点击浏览器的后退按钮后,我想显示
Confirm Form Resubmission
并将他重定向到登录,在那里他可以登录他们的帐户。
这是我的代码:
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");
});
谁能帮我实现上面列出的想法。谢谢你,问候,
请尝试一次
在Startup.cs
services.AddSession(options =>
{
options.Cookie.Name = "ExampleSession";
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.IsEssential = true;
});
在控制器中
public async Task<IActionResult> Logout()
{
try
{
await HttpContext.SignOutAsync("ExampleSession");
return RedirectToAction("Login", "Login");
}
catch (Exception ex)
{
throw ex;
}
}
尝试在 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>
我是ASP.NET CORE的新人,所以请理解我。
我目前正在为 .Net Core 中的应用程序开发登录和注销流程。
我的问题是:
- 注销后,会话不会被放弃或删除。
- 注销后,如果我点击浏览器的后退按钮,它将被重定向到您触发注销按钮的页面。
这些是我的想法,但我不知道怎么做。
- 如果用户注销,会话将被删除并放弃。
- 用户点击注销然后点击浏览器的后退按钮后,我想显示
Confirm Form Resubmission
并将他重定向到登录,在那里他可以登录他们的帐户。
这是我的代码:
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");
});
谁能帮我实现上面列出的想法。谢谢你,问候,
请尝试一次
在Startup.cs
services.AddSession(options =>
{
options.Cookie.Name = "ExampleSession";
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.IsEssential = true;
});
在控制器中
public async Task<IActionResult> Logout()
{
try
{
await HttpContext.SignOutAsync("ExampleSession");
return RedirectToAction("Login", "Login");
}
catch (Exception ex)
{
throw ex;
}
}
尝试在 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>