首页 > 解决方案 > 如何从我的 asp.net 核心应用程序中永久注销用户?

问题描述

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

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

我的问题是:

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

这些是我的想法,但我不知道如何去做。

  1. 如果用户注销,会话将被删除并放弃。
  2. 用户单击注销然后单击浏览器的后退按钮后,我想显示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");
    });

有人可以帮助我实现上面列出的想法。谢谢和问候,

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

解决方案


尝试在 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>

推荐阅读