public async Task <ActionResult> ResetPassword(string token) { string key = Request.Query["key"]; OktaHelper oktaHelper = new OktaHelper(_Config); HttpResponseMessage response = await oktaHelper.VerifyRecoveryToken(token); if (response.IsSuccessStatusCode) { var jObj = oktaHelper.GetJsonObject(response); var userId = jObj["_embedded"]["user"]["id"].Value <string>(); ResetPasswordModel resetPassword = new ResetPasswordModel() { UserId = userId, Key = Request.Query["key"] }; return(View($"~/Views/Account/ResetPassword.cshtml", resetPassword)); } else { var jObj = oktaHelper.GetJsonObject(response); string errorMessage = jObj["errorSummary"].Value <string>(); TempData["Message"] = errorMessage; return(RedirectToAction("ResendActivationLink", new { key = Request.Query["key"] })); } }
public async Task <IActionResult> SignUp(RegistrationModel register) { if (ModelState.IsValid) { register.profile.login = register.profile.email; OktaHelper oktaHelper = new OktaHelper(_Config); var result = await oktaHelper.CreateUser(register); if (result.IsSuccessStatusCode) { var user = oktaHelper.GetJsonObject(result); var activationLink = $"{Request.Scheme}://{Request.Host}/ACRLoginPortal/Account/Activate?userID={user["id"].Value<string>()}&key={register.Key}"; //Body = $"<p>Hi {register.profile.firstName},<br/><br/>Welcome to American College of Radiology!<br/><br/>To verify your email address and activate your account,please click the following link: <br/> <a href = \"{Request.Scheme}://{Request.Host}/Account/Activate?userID={user["id"].Value<string>()}&key={register.Key}\">Activate Account</a></p>", EmailModel email = new EmailModel { Body = EmailService.AccountActivationEmailBody(register.profile.firstName, activationLink), Subject = $"Activation URL", IsHtml = true, To = new List <string>() { user["profile"]["email"].Value <string>() }, SMTPServer = _Config.Value.SMTP_Server, SMTPPort = Convert.ToInt32(_Config.Value.SMTP_Port), SMTPUser = _Config.Value.SMTP_Username, SMTPPassword = _Config.Value.SMTP_Password, EnableSsl = _Config.Value.SMTP_EnableSSl }; EmailService.SendEmail(email); //TempData["Message"] = "Account created successfully. The account activation link has been sent to your email address. Please check the inbox."; return(RedirectToAction("SignUpMessage", "Registration", new { key = register.Key, status = "success" })); } else { var error = oktaHelper.GetJsonObject(result); ModelState.AddModelError("Error", error["errorCauses"][0]["errorSummary"].Value <string>()); return(View($"~/Views/Registration/SignUp.cshtml", register)); } } else { ModelState.AddModelError("Error", "Sorry, we found some errors.Please review the form and make corrections."); return(View($"~/Views/Registration/SignUp.cshtml", register)); } }
public async Task <ActionResult> ResetPassword(ResetPasswordModel resetPassword) { if (ModelState.IsValid) { OktaHelper oktaHelper = new OktaHelper(_Config); HttpResponseMessage response = await oktaHelper.ResetPassword(resetPassword); if (response.IsSuccessStatusCode) { //TempData["Message"] = "Password reset Success. Please click Sign In link below to log in."; return(RedirectToAction("ResetPasswordMessage", "Account", new { key = resetPassword.Key, status = "success" })); } else { JObject jObj = oktaHelper.GetJsonObject(response); string errMsg = jObj["errorCauses"][0]["errorSummary"].Value <string>().Replace("Password requirements were not met. ", ""); ModelState.AddModelError("Error", errMsg); return(View($"~/Views/Account/ResetPassword.cshtml", resetPassword)); } } else { ModelState.AddModelError("Error", "Sorry, we found some errors. Please review the form and make corrections."); return(View($"~/Views/Account/ResetPassword.cshtml", resetPassword)); } }
public async Task <ActionResult> ChangePassword(ChangePasswordModel changePasswordModel) { if (ModelState.IsValid) { OktaHelper oktaHelper = new OktaHelper(_Config); var result = await oktaHelper.ChangePassword(changePasswordModel); if (result.IsSuccessStatusCode) { //TempData["Message"] = "Password was changed successfully"; return(RedirectToAction("ChangePasswordMessage", "Account", new { key = changePasswordModel.Key, status = "success" })); } else { var error = oktaHelper.GetJsonObject(result); ModelState.AddModelError("Error", error["errorCauses"][0]["errorSummary"].Value <string>()); //ModelState.AddModelError("Error", result.ReasonPhrase); return(View($"~/Views/Account/ChangePassword.cshtml", changePasswordModel)); } } else { ModelState.AddModelError("Error", "Sorry, we found some errors. Please review the form and make corrections."); return(View($"~/Views/Account/ChangePassword.cshtml", changePasswordModel)); } }
public async Task <ActionResult> ForgotPassword(ForgotPasswordModel reactivation) { if (ModelState.IsValid) { OktaHelper oktaHelper = new OktaHelper(_Config); HttpResponseMessage response = await oktaHelper.ForgotPassword(reactivation.Email, false); if (response.IsSuccessStatusCode) { JObject jObj = oktaHelper.GetJsonObject(response);// (JObject)JsonConvert.DeserializeObject(strJson); var url = jObj["resetPasswordUrl"].Value <string>().ToString().Split('/'); var token = url[url.Length - 1]; try { var resetPasswordLink = $"{Request.Scheme}://{Request.Host}/ACRLoginPortal/Account/ResetPassword?token={token}&key={reactivation.Key}"; //Body = $"Please click the link to reset password.<a href='{Request.Scheme}://{Request.Host}/Account/ResetPassword?token={token}&key={reactivation.Key}'> Click here </a>", EmailService.SendEmail(new EmailModel { Subject = "Account password reset", To = new List <string>() { reactivation.Email }, Body = EmailService.PasswordResetEmailBody(reactivation.Email, resetPasswordLink), IsHtml = true, SMTPServer = _Config.Value.SMTP_Server, SMTPPort = Convert.ToInt32(_Config.Value.SMTP_Port), SMTPUser = _Config.Value.SMTP_Username, SMTPPassword = _Config.Value.SMTP_Password, EnableSsl = _Config.Value.SMTP_EnableSSl }); //TempData["Message"] = "Password reset link has been sent to your email adress."; return(RedirectToAction("ForgotPasswordMessage", "Account", new { key = reactivation.Key, status = "success" })); } catch { //TempData["Message"] = "Email Delivery Failed."; return(RedirectToAction("ForgotPasswordMessage", "Account", new { key = reactivation.Key, status = "fail" })); } } else { //TempData["Message"] = "Password reset link has been sent to your email adress."; return(RedirectToAction("ForgotPasswordMessage", "Account", new { key = reactivation.Key, status = "success" })); } } else { ModelState.AddModelError("Error", "Sorry, we found some errors. Please review the form and make corrections."); return(View($"~/Views/Account/ForgotPassword.cshtml", reactivation)); } }
public async Task <IActionResult> EditProfile(string key) { string userName = User.Claims .FirstOrDefault(x => x.Type == "preferred_username") ?.Value.ToString(); OktaHelper oktaHelper = new OktaHelper(_Config); var result = await oktaHelper.GetOktaUser(userName); if (result.IsSuccessStatusCode) { JObject user = oktaHelper.GetJsonObject(result); OktaUserModel oktaUser = user.ToObject <OktaUserModel>(); oktaUser.Key = key; oktaUser.profile.login = userName; return(View($"~/Views/Profile/EditProfile.cshtml", oktaUser)); } return(View()); }
public async Task <IActionResult> Login(LoginModel login) { string returnUrl = ""; string path = dp.UnprotectStr(login.Key); if (!Uri.IsWellFormedUriString(path, UriKind.Absolute)) { TempData["Message"] = "Sorry something went wrong, please try again!"; //"No valid Url detected to redirect, please initiate the request from the application."; return(View("~/Views/Error.cshtml")); } if (login.IsOktaSessionExists) { if (path.Contains("?")) { returnUrl = $"{path}&isAuthenticated=true"; } else { returnUrl = $"{path}?isAuthenticated=true"; } return(Redirect(returnUrl)); } else { if (ModelState.IsValid) { OktaHelper oktaHelper = new OktaHelper(_Config); HttpResponseMessage response = await oktaHelper.Login(login); if (response.IsSuccessStatusCode) { JObject jObj = oktaHelper.GetJsonObject(response); if (jObj["status"] != null) { var status = jObj["status"].Value <string>(); switch (status) { case "SUCCESS": var sessionToken = jObj["sessionToken"].Value <string>(); if (path.Contains("?")) { returnUrl = WebUtility.UrlEncode($"{path}&isAuthenticated=true"); } else { returnUrl = WebUtility.UrlEncode($"{path}?isAuthenticated=true"); } return(Redirect($"{_Config.Value.Okta_OrgUri}/login/sessionCookieRedirect?token={sessionToken}&redirectUrl={returnUrl}")); case "PASSWORD_EXPIRED": return(ChangePassword(login.Key, jObj["_embedded"]["user"]["id"].Value <string>())); default: break; } ModelState.AddModelError("Error", "Invalid email or password."); return(View($"~/Views/Account/Login.cshtml", login)); } else { ModelState.AddModelError("Error", "Invalid email or password."); return(View($"~/Views/Account/Login.cshtml", login)); } } else { ModelState.AddModelError("Error", "Invalid email or password."); return(View($"~/Views/Account/Login.cshtml", login)); } } else { ModelState.AddModelError("Error", "Sorry, we found some errors. Please review the form and make corrections."); return(View($"~/Views/Account/Login.cshtml", login)); } } }
public async Task <ActionResult> ResendActivationLink(ForgotPasswordModel reactivation) { if (ModelState.IsValid) { OktaHelper oktaHelper = new OktaHelper(_Config); HttpResponseMessage response = await oktaHelper.SearchUserByEmail(reactivation.Email); if (response.IsSuccessStatusCode) { JArray jArr = oktaHelper.GetJsonArray(response); if (jArr.Count == 1) { string userId = jArr[0]["id"].Value <string>(); response = await oktaHelper.ReactivateUser(userId); if (response.IsSuccessStatusCode) { JObject jobj = oktaHelper.GetJsonObject(response); var token = jobj["activationToken"].Value <string>().ToString(); try { var resetPasswordLink = $"{Request.Scheme}://{Request.Host}/ACRLoginPortal/Account/ResetPassword?token={token}&key={reactivation.Key}"; //Body = $"Please click the link to reset password.<a href='{Request.Scheme}://{Request.Host}/Account/ResetPassword?token={token}&key={reactivation.Key}'> Click here </a>", EmailService.SendEmail(new EmailModel { Subject = "Account password reset", To = new List <string>() { reactivation.Email }, Body = EmailService.PasswordResetEmailBody(reactivation.Email, resetPasswordLink), IsHtml = true, SMTPServer = _Config.Value.SMTP_Server, SMTPPort = Convert.ToInt32(_Config.Value.SMTP_Port), SMTPUser = _Config.Value.SMTP_Username, SMTPPassword = _Config.Value.SMTP_Password, EnableSsl = _Config.Value.SMTP_EnableSSl }); //TempData["Message"] = "Password reset link has been sent to your email adress."; return(RedirectToAction("ForgotPasswordMessage", new { key = reactivation.Key, status = "success" })); } catch { //TempData["Message"] = "Email Delivery Failed"; return(RedirectToAction("ForgotPasswordMessage", new { key = reactivation.Key, status = "fail" })); } } else { //TempData["Message"] = "User has been already activated. Please use Forget Password instead."; return(RedirectToAction("ForgotPasswordMessage", new { key = reactivation.Key, status = "error" })); } } else { ModelState.AddModelError("Error", "Invalid email"); return(View($"~/Views/Account/ResendActivationLink.cshtml", reactivation)); } } else { ModelState.AddModelError("Error", "Invalid email"); return(View($"~/Views/Account/ResendActivationLink.cshtml", reactivation)); } } else { ModelState.AddModelError("Error", "Sorry, we found some errors. Please review the form and make corrections."); return(View($"~/Views/Account/ResendActivationLink.cshtml", reactivation)); } }