public IActionResult AppSignUp([FromBody] SvcData aSignUpData) { if (aSignUpData == null) { return(BadRequest()); } try { string sJwToken; var vUserDataJson = AppEncrypt.DecryptText(aSignUpData.ComplexData); AppUser vNewUser = JsonSerializer.Deserialize <AppUser>(vUserDataJson); var vCheckUserByEmail = UserRepo.GetUserByEmail(vNewUser.EmailID); if (vCheckUserByEmail != null) { return(BadRequest("User with this Email already present use login or Forgot Password (if you had forgotten the password) ")); } var vCheckUserByMobile = UserRepo.GetUserByMobile(vNewUser.MobileNo); if (vCheckUserByMobile != null) { return(BadRequest("User with this Phone No already present use login or Forgot Password (if you had forgotten the password) ")); } vNewUser.PasswordHash = AppEncrypt.CreateHash(vNewUser.PasswordHash); var iNewUserId = UserRepo.InsertToGetId(vNewUser); if (iNewUserId > 0) { vNewUser.AppUserId = iNewUserId; sJwToken = GenerateJWToken(vNewUser); var vUserLogins = new UserLogin() { LoginToken = sJwToken, IssueDate = DateTime.Today, LoginDate = DateTime.Today, ExipryDate = DateTime.Today.AddDays(2), TokenStatus = TokenStatus.ValidToken.ToString(), UserId = vNewUser.AppUserId }; LoginRepo.Insert(vUserLogins); vNewUser.AccessToken = sJwToken; vNewUser.RefreshToken = sJwToken; } else { return(BadRequest("Unable to Save New User")); } string vRetData = JsonSerializer.Serialize(vNewUser); string sEncryptedData = AppEncrypt.EncryptText(vRetData); SvcData vReturnData = new SvcData() { ComplexData = sEncryptedData, JwToken = sJwToken }; return(Ok(vReturnData)); } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }
public IActionResult AppLogin([FromBody] SvcData aLoginData) { if (aLoginData == null) { return(BadRequest()); } try { string sJwToken; var vEmail = AppEncrypt.DecryptText(aLoginData.LoginEmail); var vPass = AppEncrypt.DecryptText(aLoginData.LoginPass); vPass = AppEncrypt.CreateHash(vPass); var vValidatedUser = UserRepo.GetLoginUser(vEmail, vPass); if (vValidatedUser != null) { sJwToken = GenerateJWToken(vValidatedUser); var vUserLogins = new UserLogin() { LoginToken = sJwToken, IssueDate = DateTime.Today, LoginDate = DateTime.Today, ExipryDate = DateTime.Today.AddDays(2), TokenStatus = TokenStatus.ValidToken.ToString(), UserId = vValidatedUser.AppUserId }; LoginRepo.Insert(vUserLogins); vValidatedUser.AccessToken = sJwToken; vValidatedUser.RefreshToken = sJwToken; } else { return(BadRequest("User Not Found")); } string vRetData = JsonSerializer.Serialize(vValidatedUser); string sEncryptedData = AppEncrypt.EncryptText(vRetData); SvcData vReturnData = new SvcData() { ComplexData = sEncryptedData, JwToken = sJwToken }; return(Ok(vReturnData)); } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }
public IActionResult UpdateNSendVerifiEmail([FromBody] SvcData aVerifiEmailData) { if (aVerifiEmailData == null) { return(BadRequest()); } try { var vLoginEmail = AppEncrypt.DecryptText(aVerifiEmailData.LoginEmail); var vUser = UserRepo.GetUserByEmail(vLoginEmail); if (vUser != null) { var vNewUserJson = AppEncrypt.DecryptText(aVerifiEmailData.ComplexData); var vNewUser = JsonSerializer.Deserialize <AppUser>(vNewUserJson); var vCheckUserByEmail = UserRepo.GetUserByEmail(vNewUser.UserEmail); if (vCheckUserByEmail != null) { return(BadRequest("Verification email has been already sent.")); } vUser.UserEmail = vNewUser.UserEmail; // UserEmail is changing so we need to create a new VerificationCode var vVerificationCode = $"{vUser.UserEmail}#{vUser.LastName}#{DateTime.UtcNow}"; vUser.VerificationCode = AppEncrypt.CreateHash(vVerificationCode); UserRepo.UpdateUserEmail(vUser); SendVerificationEmail(vUser); return(Ok()); } else { return(BadRequest("User Not Found")); } } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }
public IActionResult AppSignUp([FromBody] SvcData aSignUpData) { if (aSignUpData == null) { return(BadRequest()); } try { var vUserDataJson = AppEncrypt.DecryptText(aSignUpData.ComplexData); AppUser vNewUser = JsonSerializer.Deserialize <AppUser>(vUserDataJson); var vCheckUserByEmail = UserRepo.GetUserByEmail(vNewUser.UserEmail); if (vCheckUserByEmail != null) { return(BadRequest("User with this Email already present use login or Forgot Password (if you had forgotten the password) ")); } var vCheckUserByMobile = UserRepo.GetUserByMobile(vNewUser.MobileNo); if (vCheckUserByMobile != null) { return(BadRequest("User with this Phone No already present use login or Forgot Password (if you had forgotten the password) ")); } vNewUser.PasswordHash = AppEncrypt.CreateHash(vNewUser.PasswordHash); vNewUser.UserRole = AppConstants.AppUseRole; var vVerificationCode = $"{vNewUser.UserEmail}#{vNewUser.LastName}#{DateTime.UtcNow}"; vNewUser.VerificationCode = AppEncrypt.CreateHash(vVerificationCode); var vNewUserId = UserRepo.InsertToGetId(vNewUser); if (vNewUserId > 0) { vNewUser.AppUserId = vNewUserId; SendVerificationEmail(vNewUser); } else { return(BadRequest("Unable to Save New User")); } return(Ok()); } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }
public IActionResult SendPasswordResetEmail([FromBody] SvcData aPasswordResetData) { if (aPasswordResetData == null) { return(BadRequest()); } try { var vLoginEmail = AppEncrypt.DecryptText(aPasswordResetData.LoginEmail); //var vLoginEmail = aPasswordResetData.LoginEmail; var vAppUser = UserRepo.GetUserByEmail(vLoginEmail); if (vAppUser != null) { var vVerificationCode = $"{vAppUser.UserEmail}#{vAppUser.LastName}#{DateTime.UtcNow}"; vAppUser.VerificationCode = AppEncrypt.CreateHash(vVerificationCode); UserRepo.Update(vAppUser); var vPasswordResetUrl = $"{AppSettings.WebAppBaseUrl}/ReSetPass/{vAppUser.VerificationCode}"; var vSubject = "Password reset link"; using var vStreamReader = System.IO.File.OpenText("EmailTemplates/ResetPassword.html"); var vTemplate = vStreamReader.ReadToEnd(); vTemplate = vTemplate.Replace("{PageTitle}", vSubject); vTemplate = vTemplate.Replace("{FirstName}", vAppUser.FirstName); vTemplate = vTemplate.Replace("{VerifyUrl}", vPasswordResetUrl); EmailService.SendEmail(vAppUser.FullName, vAppUser.UserEmail, vSubject, vTemplate); return(Ok()); } else { return(BadRequest("User Not Found")); } } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }
public IActionResult ResetPassword([FromBody] SvcData aPasswordResetData) { if (aPasswordResetData == null) { return(BadRequest()); } try { var vVerificationCode = AppEncrypt.DecryptText(aPasswordResetData.VerificationCode); //var vVerificationCode = aPasswordResetData.VerificationCode; var vAppUser = UserRepo.GetUserByVerificationCode(vVerificationCode); if (vAppUser != null) { var vUserDataJson = AppEncrypt.DecryptText(aPasswordResetData.ComplexData); var vNewUser = JsonSerializer.Deserialize <AppUser>(vUserDataJson); //var vNewUser = JsonSerializer.Deserialize<AppUser>(aPasswordResetData.ComplexData); vAppUser.PasswordHash = AppEncrypt.CreateHash(vNewUser.PasswordHash); vAppUser.IsVerified = true; vAppUser.VerificationCode = null; UserRepo.Update(vAppUser); return(Ok()); } else { return(BadRequest("Invalid verification code (Link Expired)")); } } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }