Ejemplo n.º 1
1
        private void button2_Click(object sender, EventArgs e)
        {
            key = Base32Encoder.Decode(textBox2.Text);
            otp = new Totp(key);
            label3.Text = otp.ComputeTotp();

            timer1.Enabled = true;
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            key = KeyGeneration.GenerateRandomKey(40);

            textBox1.Text = Base32Encoder.Encode(key);

            otp = new Totp(key);
            label1.Text = otp.ComputeTotp();

            var url = KeyUrl.GetTotpUrl(key, "VERYVERYVERYVERYLONG");
            label2.Text = url;

            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.PureBarcode = false;
            writer.Options.Margin = 0;
            writer.Options.Width = 175;
            writer.Options.Height = 175;
            var matrix = writer.Encode(url);
            pictureBox1.Image = render.Render(matrix, BarcodeFormat.QR_CODE, string.Empty);
            pictureBox1.Invalidate();

            timer1.Enabled = true;
        }
Ejemplo n.º 4
0
        public async Task<ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            string returnUrl = TempData["returnUrl"] == null ? "" : TempData["returnUrl"].ToString();
            string userName = TempData["returnUrl"] == null ? "" : TempData["userName"].ToString();
            if (ModelState.IsValid)
            {
                byte[] secretKey = Base32Encoder.Decode(model.SecretKey);

                long timeStepMatched = 0;
                var otp = new Totp(secretKey);
                if (otp.VerifyTotp(model.Code, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    var user = await UserManager.FindByNameAsync(userName);
                    user.IsGoogleAuthenticatorEnabled = true;
                    user.TwoFactorEnabled = true;
                    user.GoogleAuthenticatorSecretKey = model.SecretKey;
                    await UserManager.UpdateAsync(user);

                    return Redirect(returnUrl);
                }
                else
                    ModelState.AddModelError("Code", "The Code is not valid");
            }

            TempData["returnUrl"] = returnUrl;
            return View(model);
        }
Ejemplo n.º 5
0
        public async Task<ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                byte[] secretKey = Base32Encoder.Decode(model.SecretKey);

                long timeStepMatched = 0;
                var otp = new Totp(secretKey);
                if (otp.VerifyTotp(model.Code, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                    user.IsGoogleAuthenticatorEnabled = true;
                    user.GoogleAuthenticatorSecretKey = model.SecretKey;
                    await UserManager.UpdateAsync(user);

                    return RedirectToAction("Index", "Manage");
                }
                else
                    ModelState.AddModelError("Code", "The Code is not valid");
            }
                
            return View(model);
        }
        private bool ValidateProofData(IProofData proofData, IAuthenticationContext authContext)
        {
            if (proofData == null || proofData.Properties == null || !proofData.Properties.ContainsKey(TOKEN_FORM_FIELD_NAME))
            {
                throw new ExternalAuthenticationException(Resources.TokenNotFoundMessage, authContext);
            }

            string key = GetEncodedSecretKey(this.upn);

            Totp otp = new Totp(Base32.Base32Encoder.Decode(key));

            long step;

            bool isVerified = otp.VerifyTotp((string)proofData.Properties[TOKEN_FORM_FIELD_NAME], out step, new VerificationWindow(previous: 1, future: 1));

            if (!isVerified) { return false; }

            string cacheKey = this.upn + "_" + step.ToString();

            if (this.cache.Get(cacheKey) != null)
            {
                throw new ExternalAuthenticationException(String.Format(Resources.TokenAlreadyUsedMessage, "[" + this.upn + "] [" + step.ToString() + "]"), authContext);
            }
            else
            {
                var policy = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.Add(new TimeSpan(0, 1, 0)) };
                this.cache.AddOrGetExisting(new CacheItem(cacheKey, "used"), policy);
            }

            return true;
        }
 private bool VerifyCode(byte[] secretKey, string code)
 {
     long timeStepMatched;
     var otp = new Totp(secretKey);
     return otp.VerifyTotp(code, out timeStepMatched, new VerificationWindow(2, 2));
 }
Ejemplo n.º 8
0
        public async Task<ActionResult> Validate(ValidateTotpViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var user = await this.UserManager.FindByIdAsync(this.User.Identity.GetUserId());
                model.IsTotpEnabled = user.IsTotpEnabled();
                var totp = new Totp(user.TotpSecretKey);
                long timeStep;
                model.CodeIsValid = totp.VerifyTotp(model.Code, out timeStep, new VerificationWindow(2, 2));
                model.TimeStepMatched = timeStep;
            }

            return this.View(model);
        }