成功登錄後返回「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 });
}
}
奇怪的是,在第一次嘗試,我得到這個問題,並在第二次嘗試它工作正常(完全返回的結果)..我再次登出後再試..第一時間內得到問題的第二次嘗試它的作品。
不能跟蹤這種故障,什麼都可以解決這個
我試圖重定向..但問題是。如果我刷新URL(我已重定向到獲取代碼作爲輸入)..它重新發送驗證短信...不安全.. – NMathur
在此處報告.. https:// github。 com/aspnet/Identity/issues/1413 – NMathur
是的,此方法依賴於密碼登錄中的cookie,因此直到下一個請求才會生效,請參閱https://docs.microsoft.com/en-us/aspnet/身份/概述/ features-api/two-factor-authentication-using-sms-and-email-with-aspnet-identity關於它的典型用法 –