public async Task <ReturnStatus> SendEmailVarification(string emailAdress) { ReturnStatus returnStatus = new ReturnStatus() { IsSuccessful = false, }; string confirmationToken = await userSvc.GetVerificationCodeAsync(emailAdress); ShimMathUser user = await userSvc.GetUserByEmailAsync(emailAdress); VerifyEmailModel model = new VerifyEmailModel() { UserName = user.Username, ConfirmationUrl = Url.ActionLink( "ConfirmEmail/" + user.ID + "/" + confirmationToken, "Account", protocol: HttpContext.Request.Scheme), }; string emailView = await ControllerExtensions.RenderViewAsync <VerifyEmailModel>(this, "/Views/Email/VerifyEmail.cshmtl", model, true); if (!string.IsNullOrEmpty(emailView)) { returnStatus = await userSvc.SendVerificationEmailAsync(emailAdress, emailView); } return(returnStatus); }
//returns ReturnStatus object with error message explaining why the user object is invalid //error message is empty if the object is valid and IsSuccessful is true too. private ReturnStatus IsValidUserObject(ShimMathUser user) { ReturnStatus retVal = new ReturnStatus() { IsSuccessful = true, ErrorMessages = new List <string>(), }; passwordDoesNotHaveCorrectFormat(user.Password); return(retVal); }
public async Task <ReturnStatus> RegisterAsync(ShimMathUser newUser) { ReturnStatus retVal = new ReturnStatus() { IsSuccessful = true, ErrorMessages = new List <string>(), }; retVal = IsValidUserObject(newUser); IdentityResult result = new IdentityResult(); if (retVal.IsSuccessful) { IdentityUser user = new IdentityUser { UserName = newUser.Username, Email = newUser.Email }; result = await UserManager.CreateAsync(user, newUser.Password); if (result.Succeeded == false) { foreach (IdentityError error in result.Errors) { retVal.ErrorMessages.Add(error.Description); } retVal.IsSuccessful = false; } else { result = await UserManager.SetLockoutEnabledAsync(user, false); if (result.Succeeded == false) { foreach (IdentityError error in result.Errors) { retVal.ErrorMessages.Add(error.Description); } retVal.IsSuccessful = false; } } } return(retVal); }
public async Task <ShimMathUser> GetUserByEmailAsync(string emailAdress) { IdentityUser identityUser = await UserManager.FindByEmailAsync(emailAdress); ShimMathUser user = null; if (identityUser != null) { user = new ShimMathUser() { ID = identityUser.Id, Email = identityUser.Email, Username = identityUser.UserName, Password = null, }; } return(user); }
public async Task <IActionResult> Login([FromBody] LoginModel loginModel) { ObjectResult retVal = BadRequest(400); ShimMathUser loginUser = new ShimMathUser() { Username = loginModel.Username, Email = loginModel.Email, Password = loginModel.Password, }; ReturnStatus returnStatus = await userSvc.Login(loginUser); if (returnStatus.IsSuccessful) { //await userSvc.Login(newUser); retVal = Ok(returnStatus); } else { retVal = BadRequest(returnStatus); } return(retVal); }
public async Task <ReturnStatus> Logout(ShimMathUser shimMathUser) { ReturnStatus retVal = new ReturnStatus() { IsSuccessful = true, }; IdentityUser user = new IdentityUser { Id = shimMathUser.ID, UserName = shimMathUser.Username, Email = shimMathUser.Email, }; //if (await SignInManager.IsSignedIn()) //{ //} await SignInManager.SignOutAsync(); retVal.IsSuccessful = false; return(retVal); }
public async Task <ReturnStatus> Login(ShimMathUser shimMathUser) { ReturnStatus retVal = new ReturnStatus() { IsSuccessful = true, ErrorMessages = new List <string>(), }; IdentityUser user = new IdentityUser { //Id = shimMathUser.ID, UserName = shimMathUser.Username, Email = shimMathUser.Email }; Microsoft.AspNetCore.Identity.SignInResult result = await SignInManager.PasswordSignInAsync(user, shimMathUser.Password, false, false); if (result.Succeeded == false) { if (result.IsLockedOut == true) { retVal.IsSuccessful = false; retVal.ErrorMessages.Add(ErrorCodeConstants.ERROR_ACCOUNT_IS_LOCKED); } else if (result.IsNotAllowed) { retVal.IsSuccessful = false; retVal.ErrorMessages.Add(ErrorCodeConstants.ERROR_ACCOUNT_NOT_ALLOWED); } else { retVal.IsSuccessful = false; retVal.ErrorMessages.Add(ErrorCodeConstants.ERROR_WRONG_PASSWORD); } } return(retVal); }
public async Task <IActionResult> Register([FromBody] RegisterModel registerModel) { ObjectResult retVal = BadRequest(400); ShimMathUser newUser = new ShimMathUser() { Username = registerModel.Username, Email = registerModel.Email, Password = registerModel.Password, }; ReturnStatus registerReturnStatus = await userSvc.RegisterAsync(newUser); if (registerReturnStatus.IsSuccessful) { ReturnStatus emailSendReturnStatus = await SendEmailVarification(newUser.Email); retVal = Ok(registerReturnStatus); } else { retVal = BadRequest(registerReturnStatus); } return(retVal); //return Ok(returnStatus); }