コード例 #1
0
        public async Task <JsonResult> GetTwoFactorDataFor(int hidrogenianId)
        {
            _logger.LogInformation("AccountController.GetTwoFactorDataFor - hidrogenianId=" + hidrogenianId);

            var secretKey = await _accountService.RetrieveTwoFaSecretKeyFor(hidrogenianId);

            if (secretKey == null)
            {
                return(new JsonResult(new { Result = RESULTS.FAILED, Message = "Error occurred while looking for your Two-Factor Authentication." }));
            }

            if (secretKey.Length == 0)
            {
                return(new JsonResult(new { Result = RESULTS.SUCCESS }));
            }

            var twoFa = new TwoFaVM {
                Id = hidrogenianId
            };
            var identity = await _accountService.GetAccountIdentity(hidrogenianId);

            var tfa           = new TwoFactorAuthenticator();
            var authenticator = tfa.GenerateSetupCode(
                HidroConstants.PROJECT_NAME, identity.Email,
                secretKey, false, 200
                );

            twoFa.QrImageUrl   = authenticator.QrCodeSetupImageUrl;
            twoFa.ManualQrCode = authenticator.ManualEntryKey;

            return(new JsonResult(new { Result = RESULTS.SUCCESS, Message = twoFa }));
        }
コード例 #2
0
        public async Task <JsonResult> EnableTwoFactorAuthentication(TwoFaVM twoFa)
        {
            _logger.LogInformation("AccountController.EnableTwoFactorAuthentication - hidrogenianId=" + twoFa.Id);

            var validation = await _reCaptchaService.IsHumanRegistration(twoFa.CaptchaToken);

            if (!validation.Result)
            {
                return(new JsonResult(validation));
            }

            var secretKey = HelperProvider.GenerateRandomString(12);

            var saved = await _userService.SaveTwoFaSecretKeyFor(twoFa.Id, secretKey);

            if (!saved.HasValue || !saved.Value)
            {
                return(new JsonResult(new { Result = RESULTS.FAILED, Message = "Error occurred while attempting to setup Two-Factor Authentication at the moment. Please try again." }));
            }

            var identity = await _accountService.GetAccountIdentity(twoFa.Id);

            var tfa = new TwoFactorAuthenticator();

            var authenticator = tfa.GenerateSetupCode(
                HidroConstants.PROJECT_NAME, identity.Email,
                secretKey, false, 200
                );

            twoFa.QrImageUrl   = authenticator.QrCodeSetupImageUrl;
            twoFa.ManualQrCode = authenticator.ManualEntryKey;

            return(new JsonResult(new { Result = RESULTS.SUCCESS, Message = twoFa }));
        }
コード例 #3
0
        public async Task <JsonResult> DisableTwoFactorAuthentication(TwoFaVM twoFa)
        {
            _logger.LogInformation("AccountController.DisableTwoFactorAuthentication - hidrogenianId=" + twoFa.Id);

            var validation = await _reCaptchaService.IsHumanRegistration(twoFa.CaptchaToken);

            if (!validation.Result)
            {
                return(new JsonResult(validation));
            }

            var updated = await _userService.RemoveTwoFaSecretKeyFor(twoFa.Id);

            return(!updated.HasValue ? new JsonResult(new { Result = RESULTS.FAILED, Message = "Unable to find your account with the given data. Please login again and try." })
                                     : (!updated.Value ? new JsonResult(new { Result = RESULTS.FAILED, Message = "Error occurred while removing your Two-Factor Authentication data. Please try again." })
                                                       : new JsonResult(new { Result = RESULTS.SUCCESS })));
        }