Пример #1
0
        public async Task<ActionResult> EnableTotp(EnableTotpViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var key = Base32Encoder.Decode(model.Key);
                var totp = new Totp(key);
                long timeStep;
                if (totp.VerifyTotp(model.Code, out timeStep, new VerificationWindow(2, 2)))
                {
                    var user = await this.UserManager.FindByIdAsync(this.User.Identity.GetUserId());
                    user.EnableTotp(key);
                    var results = await this.UserManager.UpdateAsync(user);
                    if (results.Succeeded)
                    {
                        await this.SignInAsync(user, false);
                        return this.RedirectToAction("Index");                        
                    }

                    foreach (var error in results.Errors)
                    {
                        this.ModelState.AddModelError(string.Empty, error);
                    }
                }
            }

            return this.View(model);
        }
Пример #2
0
        public ActionResult EnableTotp()
        {
            var name = this.User.Identity.GetUserName();
            var key = KeyGeneration.GenerateRandomKey(20);
            var barcodeUrl = KeyUrl.GetTotpUrl(key, name) + "&issuer=AspNetTotp";

            var model = new EnableTotpViewModel { Key = Base32Encoder.Encode(key), TotpUrl = barcodeUrl };

            return this.View(model);
        }