c# - 如何从我的 ASP.NET Core 应用程序中永久注销用户?

标签 c# asp.net-core asp.net-core-mvc

我是 ASP.NET CORE 的新手,所以请理解我。

我目前正在为 .Net Core 中的应用程序开发登录和注销流程。

我的问题是:

  1. 注销后, session 不会被放弃或删除。
  2. 注销后,如果我点击浏览器的后退按钮,它将重定向到您触发注销按钮的页面。

这些是我的想法,但我不知道如何实现。

  1. 如果用户退出, session 将被删除并放弃。
  2. 用户单击“注销”,然后单击浏览器的后退按钮后,我想显示确认表单重新提交并将其重定向到“登录”,以便他可以登录其帐户。

这是我的代码:

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/

相关文章:

c# - ASP.NET Core 以 EF6 和 Identity 为目标的完整框架

c# - 将 RDL 转换为 RDLC 的最佳实践

asp.net-core - ASP.NET Core 模板 3.1.5 - serviceDependencies.json

c# - 如何在.Net Core 2.2中解析Microsoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValue

asp.net-core - 如何在 Controller 内获取 @Url.Action 值

linux - 如何在 debian/linux 上的 Asp.Net Core 2 上使用证书保护数据保护 key 文件

c# - 如何验证 GET 方法中的查询字符串参数

c# - 可扩展性位置距离搜索全美国超过 100,000 个 LatLng 位置

c# - 如何在 Action 中区分 ajax 请求和常规请求?

c# - 使用 System.Data.Sqlite 进行 Sqlite 在线备份