Anti-Forgery Token was meant for a different claims-based user

前端 未结 7 1762
有刺的猬
有刺的猬 2020-12-28 13:41

I am working on a logout feature in the application we are using ASP.NET Identity login. I can login successfully but when I logout and then try to login again I get the fol

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 14:20

    I've been getting this same error on the login for a LONG time now, but haven't been able to work out why. Finally I found it, so I'm posting it here (although it's a slightly different cause) in case someone else has it.

    This was my code:

    //
    // GET: /login
    [OutputCache(NoStore = true, Location = System.Web.UI.OutputCacheLocation.None)]
    public ActionResult Login()
    {
        return View();
    }
    
    //
    // POST: /login
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task Login(LoginViewModel model, string returnUrl)
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        //etc...
    

    This worked fine for 99.99% of the logins, but every now & then I got the above-mentioned error, although I couldn't reproduce it, until now.

    The error only happens when someone clicks the login button twice in quick succession. However, if I remove the AuthenticationManager.SignOut line in the Login action, then it's fine. I'm not sure why I put that line in there, but it's causing the issue - and removing it fixes the problem.

提交回复
热议问题