2017-08-20 87 views
2

成功登錄後返回「UserId not found」我通過依賴注入使用SignInManager和AuthenticationManager。除了所有工作的罰款SendTwoFactorCodeAsyncSendTwoFactorCodeAsync在成功登錄後使用依賴關係使用SignManager

下面是代碼:

UtilityConfig

container.RegisterType<ApplicationSignInManager>(); 
container.RegisterType<ApplicationUserManager>(); 

賬戶控制器

var myUser = await UserManagerService.Value.FindAsync(model.Mobile, model.Password); 
if (myUser != null) 
    { 
     var result = await SignInManager.Value.PasswordSignInAsync(model.Mobile, model.Password, false, shouldLockout: false); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       { 
        //success code 
       } 
      case SignInStatus.RequiresVerification: 
       { 
        var code = await UserManagerService.Value.GenerateTwoFactorTokenAsync(myUser.Id, ConfigurationManager.AppSettings["PhoneCodeProvider"]); 
        var send = await SignInManager.Value.SendTwoFactorCodeAsync(ConfigurationManager.AppSettings["PhoneCodeProvider"]); //here throughing exception 
        if (send) 
         { 
          SmsHelper.Value.SendSmsBySmsBatch(user.UserName, Constant.Instance().ProductName, myUser.UserName, "Code is " + code, true); 
          return RedirectToAction("TwoFactorCode", new { Provider = ConfigurationManager.AppSettings["PhoneCodeProvider"], ReturnUrl = returnUrl, RememberMe = false }); 
         }    
       } 
       break; 
       ---------------        
         } 

也試過這樣:

var userId = await SignInManager.Value.GetVerifiedUserIdAsync(); // this is returning 0 
    var user = await UserManagerService.Value.FindByIdAsync(userId); 
    if (user != null) 
    { 
     var code = await UserManagerService.Value.GenerateTwoFactorTokenAsync(myUser.Id, ConfigurationManager.AppSettings["PhoneCodeProvider"]); 
     var send = await SignInManager.Value.SendTwoFactorCodeAsync(ConfigurationManager.AppSettings["PhoneCodeProvider"]); //here throughing exception 
     if (send) 
      { 
       SmsHelper.Value.SendSmsBySmsBatch(user.UserName, Constant.Instance().ProductName, user.UserName, "Your security code is " + code, true); 
       return RedirectToAction("TwoFactorCode", new { Provider = ConfigurationManager.AppSettings["PhoneCodeProvider"], ReturnUrl = returnUrl, RememberMe = false }); 
      } 
    } 

奇怪的是,在第一次嘗試,我得到這個問題,並在第二次嘗試它工作正常(完全返回的結果)..我再次登出後再試..第一時間內得到問題的第二次嘗試它的作品。

不能跟蹤這種故障,什麼都可以解決這個

回答

1

我得到確切同樣的錯誤的最好方法的原因。我試圖添加案例SignInStatus.RequiresVerification:Thread.Sleep(500) 看看它是否只是一個延遲,但無濟於事。

它需要一個重定向,因爲SendTwoFactorCodeAsync會檢查Request.IsAuthernticated。 。該請求沒有,但有一個有效的日誌中

這是一個錯誤,應該在GitHub上

+0

我試圖重定向..但問題是。如果我刷新URL(我已重定向到獲取代碼作爲輸入)..它重新發送驗證短信...不安全.. – NMathur

+0

在此處報告.. https:// github。 com/aspnet/Identity/issues/1413 – NMathur

+0

是的,此方法依賴於密碼登錄中的cookie,因此直到下一個請求才會生效,請參閱https://docs.microsoft.com/en-us/aspnet/身份/概述/ features-api/two-factor-authentication-using-sms-and-email-with-aspnet-identity關於它的典型用法 –

0

此報告是我的代碼:

 [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

     // This doesn't count login failures towards account lockout 
     // To enable password failures to trigger account lockout, change to shouldLockout: true 
     var result = await SignInManager.PasswordSignInAsync(model.Email.Trim(), model.Password.Trim(), model.RememberMe, shouldLockout: false); 
     switch (result) 
     { 
      case SignInStatus.Success: 
       return RedirectToLocal(returnUrl); 
      case SignInStatus.LockedOut: 
       return View("Lockout"); 
      case SignInStatus.RequiresVerification: 
       var userId = await SignInManager.GetVerifiedUserIdAsync(); 
       if (userId == null) 
       { 
        return RedirectToAction("redirectSendTwoFactorCodeAsync", "Account", 
         new 
         { 
          ReturnUrl = returnUrl, 
          Email = model.Email, 
          Password = model.Password, 
          RememberMe = model.RememberMe 
         }); 
       } 

       if (!await SignInManager.SendTwoFactorCodeAsync(twoPartCode)) 
       { 
        ModelState.AddModelError("", "Failure sending Email verification token"); 
        return View(model); 
       } 
       return View("VerifyCode", new VerifyCodeViewModel { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
      case SignInStatus.Failure: 
      default: 
       ModelState.AddModelError("", "Invalid login attempt."); 
       return View(model); 
     } 
    } 

    [HttpGet] 
    [AllowAnonymous] 
    public async Task<ActionResult> redirectSendTwoFactorCodeAsync(string ReturnUrl, string Email, string Password, bool RememberMe) 
    { 
     if (!await SignInManager.SendTwoFactorCodeAsync(twoPartCode)) 
     { 
      ModelState.AddModelError("", "Failure sending Email verification token"); 
      return View("Login", new LoginViewModel() 
      { 
       Email = Email, 
       Password = Password, 
       RememberMe = RememberMe 
      }); 
     } 
     return View("VerifyCode", new VerifyCodeViewModel { ReturnUrl = ReturnUrl, RememberMe = RememberMe }); 
    }