public async Task <IActionResult> EnableAuthenticator(EnableAuthenticatorViewModel model) { var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } if (!ModelState.IsValid) { await LoadSharedKeyAndQrCodeUriAsync(user, model); return(View(model)); } // Strip spaces and hypens var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty); var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync( user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode); if (!is2faTokenValid) { ModelState.AddModelError("Code", "Verification code is invalid."); await LoadSharedKeyAndQrCodeUriAsync(user, model); return(View(model)); } await _userManager.SetTwoFactorEnabledAsync(user, true); _logger.LogInformation("User with ID {UserId} has enabled 2FA with an authenticator app.", user.Id); var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); TempData[RecoveryCodesKey] = recoveryCodes.ToArray(); return(RedirectToAction(nameof(ShowRecoveryCodes))); }
public async Task <IActionResult> EnableAuthenticator(EnableAuthenticatorViewModel model) { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound(_sharedLocalizer["USER_NOTFOUND", _userManager.GetUserId(User)])); } if (!ModelState.IsValid) { await LoadSharedKeyAndQrCodeUriAsync(user, model); return(View(model)); } // Strip spaces and hypens var verificationCode = model.Code.Replace(" ", string.Empty).Replace("-", string.Empty); var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync( user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode); if (!is2faTokenValid) { ModelState.AddModelError("Code", _sharedLocalizer["INVALID_VERFICATION_CODE"]); await LoadSharedKeyAndQrCodeUriAsync(user, model); return(View(model)); } await _userManager.SetTwoFactorEnabledAsync(user, true); _logger.LogInformation("User with ID {UserId} has enabled 2FA with an authenticator app.", user.Id); var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); TempData[RecoveryCodesKey] = recoveryCodes.ToArray(); return(RedirectToAction(nameof(ShowRecoveryCodes))); }
public async Task<IActionResult> EnableAuthenticator() { var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } var model = new EnableAuthenticatorViewModel { SharedKey = FormatKey(unformattedKey), AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey) }; return View(model); }
public async Task <IActionResult> EnableAuthenticator() { await OnGetSesstion(); var user = await _userManager.GetUserAsync(User); var userPersonalRow = _context.UserPersonalDetail.Where(a => a.UserId == user.Id).SingleOrDefault(); if (userPersonalRow == null) { return(RedirectToAction("CompleteProfile", "Home")); } if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var model = new EnableAuthenticatorViewModel(); await LoadSharedKeyAndQrCodeUriAsync(user, model); return(View(model)); }
public async Task <IActionResult> EnableAuthenticator(EnableAuthenticatorViewModel ViewModel) { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound()); } if (!ModelState.IsValid) { return(View(await LoadSharedKeyAndQrCodeUriAsync(user))); } var VerificationCode = ViewModel.Code.Replace(" ", string.Empty).Replace("-", string.Empty); var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, ViewModel.Code); if (!is2faTokenValid) { ModelState.AddModelError(string.Empty, "کد اعتبارسنجی نامعتبر است."); return(View(await LoadSharedKeyAndQrCodeUriAsync(user))); } await _userManager.SetTwoFactorEnabledAsync(user, true); if (await _userManager.CountRecoveryCodesAsync(user) == 0) { var RecoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); ViewBag.Alert = "اپلیکشن احراز هویت شما تایید شده است"; return(View("ShowRecoveryCodes", RecoveryCodes)); } else { return(RedirectToAction("TwoFactorAuthentication", new { alert = "success" })); } }
public async Task <IActionResult> EnableAuthenticator(EnableAuthenticatorViewModel model) { if (model == null) { return(NotFound()); } if (!ModelState.IsValid) { return(View(model)); } var user = await _userManager.GetUserAsync(User).ConfigureAwait(false); if (user == null) { throw new ApplicationException($"Unable to load user with Id '{_userManager.GetUserId(User)}'."); } // Strip spaces and hypens var verificationCode = model.Code .Replace(" ", string.Empty, StringComparison.InvariantCultureIgnoreCase) .Replace("-", string.Empty, StringComparison.InvariantCultureIgnoreCase); var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync( user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode).ConfigureAwait(false); if (!is2faTokenValid) { ModelState.AddModelError("model.Code", "Verification code is invalid."); return(View(model)); } await _userManager.SetTwoFactorEnabledAsync(user, true).ConfigureAwait(false); _logger.LogInformation(_localizer["User with Id {UserId} has enabled 2FA with an authenticator app."], user.Id); return(RedirectToAction(nameof(GenerateRecoveryCodes))); }
private async Task LoadSharedKeyAndQrCodeUriAsync(Microsoft.AspNetCore.Identity.MongoDB.IdentityUser user, EnableAuthenticatorViewModel model) { var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } model.SharedKey = FormatKey(unformattedKey); model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey); }
private async System.Threading.Tasks.Task LoadSharedKeyAndQrCodeUriAsync(ApplicationUser user, EnableAuthenticatorViewModel model) { var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } model.SharedKey = FormatKey(unformattedKey); model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey); }
public async Task <IActionResult> EnableAuthenticator() { try { var user = await GetCurrentUserAsync(); //// Update Status string oldvalue = JsonConvert.SerializeObject(user); //user.TwoFactorEnabled = true; //await _userManager.UpdateAsync(user); //// Update Status //return Ok(new TwoFactorAuthResponse { ReturnCode = enResponseCode.Success, ReturnMsg = EnResponseMessage.EnableTroFactor }); var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); if (string.IsNullOrEmpty(unformattedKey)) { await _userManager.ResetAuthenticatorKeyAsync(user); unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); } string Newvalue = JsonConvert.SerializeObject(user); UserChangeLogViewModel userChangeLogViewModel = new UserChangeLogViewModel(); userChangeLogViewModel.Id = user.Id; userChangeLogViewModel.Newvalue = Newvalue; userChangeLogViewModel.Type = EnuserChangeLog.TwofactoreChange.ToString(); userChangeLogViewModel.Oldvalue = oldvalue; long userlog = _iuserChangeLog.AddPassword(userChangeLogViewModel); TwoFactorAuth TFAuth = new TwoFactorAuth(); //string URL; string sKey = string.Empty; // string sName = string.Empty; //sKey = TFAuth.CreateSecret(160); // sName = user.UserName; // dSetReq.Tables(0).Rows(0)("NAME"); sKey = TFAuth.CreateSecret(160); //URL = TFAuth.GetQrCodeImageAsDataUri(sName, sKey); // string value = URL + "" + sKey; // string code123 = TFAuth.GetQrCodeImageAsDataUri(, string secret) user.PhoneNumber = sKey; var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { var model = new EnableAuthenticatorViewModel { SharedKey = FormatKey(sKey), //AuthenticatorUri = GenerateQrCodeUri(user.UserName, unformattedKey) // UserName = user.UserName, AuthenticatorUri = TFAuth.GetQrCodeImageAsDataUri(user.UserName, sKey) }; return(Ok(new EnableAuthenticationResponse { ReturnCode = enResponseCode.Success, ReturnMsg = EnResponseMessage.TwoFactorActiveRequest, EnableAuthenticatorViewModel = model })); } return(BadRequest(new EnableAuthenticationResponse { ReturnCode = enResponseCode.Fail, ReturnMsg = EnResponseMessage.TwoFactorActiveRequest, ErrorCode = enErrorCode.NotFound })); //if (string.IsNullOrEmpty(user.Email)) //// This Condition by pankaj for when user login with molile the email field is null so. //{ // var model = new EnableAuthenticatorViewModel // { // SharedKey = FormatKey(unformattedKey), // AuthenticatorUri = GenerateQrCodeUri(user.UserName, unformattedKey) // }; // return Ok(new EnableAuthenticationResponse { ReturnCode = enResponseCode.Success, ReturnMsg = EnResponseMessage.TwoFactorActiveRequest, EnableAuthenticatorViewModel = model }); //} //else //{ // var model = new EnableAuthenticatorViewModel // { // SharedKey = FormatKey(unformattedKey), // AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey) // }; // return Ok(new EnableAuthenticationResponse { ReturnCode = enResponseCode.Success, ReturnMsg = EnResponseMessage.TwoFactorActiveRequest, EnableAuthenticatorViewModel = model }); //} } catch (Exception ex) { _logger.LogError(ex, "Date: " + _basePage.UTC_To_IST() + ",\nMethodName:" + System.Reflection.MethodBase.GetCurrentMethod().Name + "\nControllername=" + this.GetType().Name, LogLevel.Error); return(BadRequest(new TwoFactorAuthResponse { ReturnCode = enResponseCode.InternalError, ReturnMsg = ex.ToString(), ErrorCode = enErrorCode.Status500InternalServerError })); } }
private Task LoadSharedKeyAndQrCodeUriAsync(ApplicationUser user, EnableAuthenticatorViewModel model) { throw new NotImplementedException(); }