public async Task <AppUser> LoginAsync(SvcData aLoginUser) { try { aLoginUser.LoginEmail = AppEncrypt.EncryptText(aLoginUser.LoginEmail); aLoginUser.LoginPass = AppEncrypt.EncryptText(aLoginUser.LoginPass); string serializedUser = JsonSerializer.Serialize(aLoginUser); var vRequestMessage = new HttpRequestMessage(HttpMethod.Post, LoginSvcUrl) { Content = new StringContent(serializedUser) }; vRequestMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(AppConstants.JsonMediaTypeHeader); var vSvcResponse = await SvcClient.SendAsync(vRequestMessage); if (vSvcResponse.IsSuccessStatusCode) { var vResponseBody = await vSvcResponse.Content.ReadAsStreamAsync(); SvcData vSvcRetObj = await JsonSerializer.DeserializeAsync <SvcData>(vResponseBody, JsonOptions); string sDeCryptedUser = AppEncrypt.DecryptText(vSvcRetObj.ComplexData); return(JsonSerializer.Deserialize <AppUser>(sDeCryptedUser, JsonOptions)); } return(null); } catch (Exception) { throw; } }
public async Task <AppUser> GetUserByAccessTokenAsync(string aSvcAccessToken) { SvcData vVerifyToken = new SvcData() { JwToken = aSvcAccessToken }; string vRefreshRequest = JsonSerializer.Serialize(vVerifyToken); var vRequestMessage = new HttpRequestMessage(HttpMethod.Post, UserByTokenSvcUrl) { Content = new StringContent(vRefreshRequest) }; vRequestMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(AppConstants.JsonMediaTypeHeader); var vSvcResponse = await SvcClient.SendAsync(vRequestMessage); var vResponseBody = await vSvcResponse.Content.ReadAsStreamAsync(); SvcData vSvcRetObj = await JsonSerializer.DeserializeAsync <SvcData>(vResponseBody, JsonOptions); string sDeCryptedUser = AppEncrypt.DecryptText(vSvcRetObj.ComplexData); return(JsonSerializer.Deserialize <AppUser>(sDeCryptedUser, JsonOptions)); }
public async Task <AppUser> VerifyEmailAsync(SvcData aVerifyEmailData) { aVerifyEmailData.VerificationCode = AppEncrypt.EncryptText(aVerifyEmailData.VerificationCode); string serializedData = JsonSerializer.Serialize(aVerifyEmailData); var vRequestMessage = new HttpRequestMessage(HttpMethod.Post, VerifyEmailSvcUrl) { Content = new StringContent(serializedData) }; vRequestMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(AppConstants.JsonMediaTypeHeader); var vSvcResponse = await SvcClient.SendAsync(vRequestMessage); if (vSvcResponse.IsSuccessStatusCode) { var vResponseBody = await vSvcResponse.Content.ReadAsStreamAsync(); SvcData vSvcRetObj = await JsonSerializer.DeserializeAsync <SvcData>(vResponseBody, JsonOptions); string sDeCryptedUser = AppEncrypt.DecryptText(vSvcRetObj.ComplexData); return(JsonSerializer.Deserialize <AppUser>(sDeCryptedUser, JsonOptions)); } else { throw new Exception(await vSvcResponse.Content.ReadAsStringAsync()); } }
public IActionResult ResendVerifiEmail([FromBody] SvcData aVerifiEmailData) { if (aVerifiEmailData == null) { return(BadRequest()); } try { var vLoginEmail = AppEncrypt.DecryptText(aVerifiEmailData.LoginEmail); var vUser = UserRepo.GetUserByEmail(vLoginEmail); if (vUser != null) { 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 { 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 static string GetConnectionFromToken(string aJwToken) { var vTokenHandler = new JwtSecurityTokenHandler(); var vTokenObject = vTokenHandler.ReadToken(aJwToken) as JwtSecurityToken; var vResult = vTokenObject.Claims.First(claim => claim.Type == ClaimTypes.Hash).Value; string sConstring = AppEncrypt.DecryptText(vResult); return(sConstring); }
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)); } }
public IActionResult VerifyEmail([FromBody] SvcData aVerifyEmailData) { if (aVerifyEmailData == null) { return(BadRequest()); } try { var vVerificationCode = AppEncrypt.DecryptText(aVerifyEmailData.VerificationCode); var vAppUser = UserRepo.GetUserByVerificationCode(vVerificationCode); if (vAppUser == null) { return(BadRequest("Invalid verification code.")); } if (!vAppUser.IsVerified) { vAppUser.IsVerified = true; vAppUser.VerificationCode = null; UserRepo.Update(vAppUser); } var vRetData = JsonSerializer.Serialize(vAppUser); var vEncryptedData = AppEncrypt.EncryptText(vRetData); var vReturnData = new SvcData { ComplexData = vEncryptedData }; return(Ok(vReturnData)); } catch (Exception ex) { AppLogger.LogCritical(ex.Message); return(BadRequest(ex)); } }
public async Task <AppUser> RegisterUserAsync(SvcData aLoginUser) { aLoginUser.ComplexData = AppEncrypt.EncryptText(aLoginUser.ComplexData); string serializedUser = JsonSerializer.Serialize(aLoginUser); var vRequestMessage = new HttpRequestMessage(HttpMethod.Post, RegSvcUrl) { Content = new StringContent(serializedUser) }; vRequestMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(AppConstants.JsonMediaTypeHeader); var vSvcResponse = await SvcClient.SendAsync(vRequestMessage); var vResponseBody = await vSvcResponse.Content.ReadAsStreamAsync(); SvcData vSvcRetObj = await JsonSerializer.DeserializeAsync <SvcData>(vResponseBody, JsonOptions); string sDeCryptedUser = AppEncrypt.DecryptText(vSvcRetObj.ComplexData); return(JsonSerializer.Deserialize <AppUser>(sDeCryptedUser, JsonOptions)); }