Asp.Net Identity with 2FA - remember browser cookie not retained after session

后端 未结 2 1419
余生分开走
余生分开走 2020-12-31 06:19

I\'m using the latest sample code for MVC5.2 with Asp.Identity and Two Factor authentication.

With 2FA enabled, when a user logins, the get prompted for a code (sent

2条回答
  •  隐瞒了意图╮
    2020-12-31 06:38

    This still appears to be an issue in Identity 2.2.1 (It may be fixed in Asp.Net Identity 3.0 - but that is currently pre-released and requires a later version of .Net framework that 4.5)

    The following work around seems ok for now: The cookie is getting set on the SignInManager.TwoFactorSignInAsync with the wrong values, so on Success of the VerifyCode action, I reset the cookie to be persistent and give it the expiry date that I wish (in this case I set it to a year)

      public async Task VerifyCode(VerifyCodeViewModel model)
      {
            if (!ModelState.IsValid)
            {
                return View(model);
            }            var result = await SignInManager.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent:  model.RememberMe, rememberBrowser: model.RememberBrowser);
            switch (result)
            {
                case SignInStatus.Success:
                    // if we remember the browser, we need to adjsut the expiry date as persisted above
                    // Also set the expiry date for the .AspNet.ApplicationCookie 
                    if (model.RememberBrowser)
                    {
                        var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync());
                        var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(user.Id);
                        AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddDays(365) }, rememberBrowserIdentity);
                    }
    
                    return RedirectToLocal(model.ReturnUrl);
    

提交回复
热议问题