【问题标题】:Google Authenticator ValidateTwoFactorPIN(UserUniqueKey, token) always false MVC 5Google Authenticator ValidateTwoFactorPIN(UserUniqueKey, token) 总是假 MVC 5
【发布时间】:2019-08-24 03:35:45
【问题描述】:

您好,我正在尝试使用 google 身份验证器实现 2FA,但我无法开始工作。

我正在关注下一篇文章 http://demo.dotnetawesome.com/two-factor-authentication-in-aspnet-mvc

我听说我必须同步代码的时间校正但是当我尝试谷歌身份验证器应用程序时说时间已经正确

有什么想法吗? 谢谢

public class HomeController : Controller
{
    private const string key = "Max@123456"; // any 10-12 char string for use as private key in google authenticator
    public ActionResult Login()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login(LoginModel login)
    {
        string message = "";
        bool status = false;

        //check username and password form our database here
        //for demo I am going to use Admin as Username and Password1 as Password static value
        if (login.Username == "Admin" && login.Password == "Password1")
        {
            status = true; // show 2FA form
            message = "2FA Verification";
            Session["Username"] = login.Username;

            //2FA Setup
            TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
            string UserUniqueKey = login.Username + key; //as Its a demo, I have done this way. But you should use any encrypted value here which will be unique value per user 
            Session["UserUniqueKey"] = UserUniqueKey;
            var setupInfo = tfa.GenerateSetupCode("NAVIGIA 2FA", login.Username, UserUniqueKey, 300, 300);
            ViewBag.BarcodeImageUrl = setupInfo.QrCodeSetupImageUrl;
            ViewBag.SetupCode = setupInfo.ManualEntryKey;
        }
        else
        {
            message = "Invalid credential";
        }
        ViewBag.Message = message;
        ViewBag.Status = status;
        return View();
    }

    public ActionResult MyProfile()
    {
        if (Session["Username"] == null || Session["IsValid2FA"] == null || !(bool)Session["IsValid2FA"])
        {
            return RedirectToAction("Login");
        }
        ViewBag.Message = "Welcome " + Session["Username"].ToString();
        return View();
    }
    public ActionResult Verify2FA()
    {
        var token = Request["passcode"];
        TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
        string UserUniqueKey = Session["UserUniqueKey"].ToString();
        bool isValid = tfa.ValidateTwoFactorPIN(UserUniqueKey, token);
        if (isValid)
        {
            Session["IsValid2FA"] = true;
            return RedirectToAction("MyProfile", "Home");
        }
        return RedirectToAction("Login", "Home");
    }
}

【问题讨论】:

    标签: asp.net-mvc authentication google-oauth


    【解决方案1】:

    我发现了我的问题。这是与时区。我设置了自动时间和时区,现在运行良好。

    感谢大家

    【讨论】:

    • 你是怎么做到的?我也面临同样的问题。
    • @Ali 检查你的电脑或服务器时区
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2019-01-30
    • 2015-12-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2019-10-11
    相关资源
    最近更新 更多