【问题标题】:ASP NET CORE MVC-Application and "Double-Logout"ASP NET CORE MVC-Application 和“双注销”
【发布时间】:2020-07-01 13:47:47
【问题描述】:

我的 ASP NET CORE 3.1 MVC 应用程序的“双重注销”存在问题。 问题是: - 运行应用程序 - 登录 - 打开同一站点的第二个浏览器窗口 - 在其中一个 Windows 中单击注销 - 现在的问题...在第二个窗口中单击注销:出现一个白色页面,url 设置为“https://localhost:44305/Identity/Account/Logout?returnUrl=%2F”并且没有任何反应...

错误出现在新建项目,使用默认VS 2019模板设置!

谁有想法?

BR马可

【问题讨论】:

  • 如果我理解,你的问题是如何从另一个浏览器访问注销浏览器会话,你不能这样做
  • 如果您可以提供您的注销代码,我们可以尝试解决此问题
  • 您需要在您的问题中在此处发布minimal reproducible example

标签: asp.net-core asp.net-core-mvc visual-studio-2019


【解决方案1】:

不,不是另一个浏览器,只是同一个浏览器的另一个标签页!

我没有注销代码,它在“默认的 asp net core 模板”里面!如果您构建一个新的 Asp Net Core MVC 应用程序,您将拥有相同的行为!

我唯一拥有的是注销表单的“代码”(由 VS2019 生成):

<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })">
    <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>

【讨论】:

    【解决方案2】:

    在第二个窗口中点击注销:出现一个白色页面,url 设置为“https://localhost:44305/Identity/Account/Logout?returnUrl=%2F”并且没有任何反应

    我可以重现同样的问题,如果我们在浏览器网络选项卡中检查请求,我们可以发现它返回 400 状态码,如下所示。

    根据我的测试和研究,问题似乎是由antiforgery token validation 在您单击第二个浏览器选项卡中的注销按钮时引起的。

    如果您想跳过防伪令牌验证,可以尝试replace the default code of Logout PageModel 并对其应用IgnoreAntiforgeryTokenAttribute,如下所示。

    [AllowAnonymous]
    [IgnoreAntiforgeryToken]
    public class LogoutModel : PageModel
    {
        private readonly SignInManager<IdentityUser> _signInManager;
        private readonly ILogger<LogoutModel> _logger;
    
        public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
        {
            _signInManager = signInManager;
            _logger = logger;
        }
    
        public void OnGet()
        {
        }
    
        public async Task<IActionResult> OnPost(string returnUrl = null)
        {
            await _signInManager.SignOutAsync();
            _logger.LogInformation("User logged out.");
            if (returnUrl != null)
            {
                return LocalRedirect(returnUrl);
            }
            else
            {
                return RedirectToPage();
            }
        }
    }
    

    测试结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      相关资源
      最近更新 更多