public async Task <IHttpActionResult> ForgotPassword([FromBody] ForgotPasswordModel model) { var user = DB.Users.Include(x => x.Claims).FirstOrDefault(x => x.Email == model.Email); if (user != null) { if (!user.EmailConfirmed || user.LockedOut) { return(BadRequest(MessageResources.ERR_INVALID_ACCOUNT_STATE)); } // generate token for user string token; var userToken = DB.Tokens.FirstOrDefault(x => x.TokenType == TokenType.PasswordReset && x.User.Id == user.Id); if (userToken != null) { token = userToken.Token; } else { token = UserTokensHelper.Generate(); UserToken dbToken = new UserToken() { TokenType = TokenType.PasswordReset, User = user, Token = token }; DB.Tokens.Add(dbToken); // Save token await DB.SaveChangesAsync(); } // send token to client via email await emailService.SendAsync(new SendMailOptions() { Destinations = new string[] { user.Email }, Subject = "Reset Password", IsHtml = false, Message = $"Hello {user.FullName.Split(' ').First()}, here's your password reset code : {token}" }); return(Ok(new { ResetToken = JwtHelper.SignToken(user, audience: TokenAudiences.ResetPassword) })); } return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, MessageResources.MSG_RESET_PWD_INVALID_USER))); }
public HttpResponseMessage getAccessTokenByRefreshToken([FromBody] RefreshTokenObject refreshTokenObj) { UserObjectModel userObj = TokenHelper.getUserByRefreshToken(refreshTokenObj.RefreshToken); TokenObjectModel tokenObj = TokenHelper.createToken(userObj); tokenObj = TokenHelper.getTokenByAuthorizationCode(tokenObj.AuthorizationCode); if (userObj != null || tokenObj != null) { UserTokensObjectModel userTokenObj = new UserTokensObjectModel(userObj.UserId, tokenObj.TokenId); bool success = UserTokensHelper.MapUserToken(userTokenObj); TokenObjectModel tokenUserObj = new TokenObjectModel(tokenObj.TokenId, tokenObj.AccessToken, tokenObj.RefreshToken, tokenObj.ExpiresIn, tokenObj.AuthorizationCode, userObj.UserId, userObj.Username, userObj.EmailId); var message = Request.CreateResponse(HttpStatusCode.Created, tokenUserObj); return(message); } else { var message = Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid Token"); return(message); } }
public async Task ConfirmPhone([FromUri] string phone) { var user = DB.Users.Include(x => x.Tokens).FirstOrDefault(x => x.Id == UserId); if (!DB.Users.Any(x => x.Phone == phone)) { // Generate and send confirmation code to the phone number string dispatchToken = null; var existingToken = user.Tokens.FirstOrDefault(x => x.TokenType == TokenType.ChangePhone); if (existingToken == null) { // Generate and send confirmation code to the phone number dispatchToken = UserTokensHelper.Generate(); // UserToken dbToken = new UserToken() { TokenType = TokenType.ChangePhone, User = user, Token = dispatchToken, }; DB.Tokens.Add(dbToken); // Save token DB.SaveChanges(); } else { dispatchToken = existingToken.Token; } // Dispatch token to user via phone await smsService.SendAsync(new SendSMSOptions() { Destinations = new string[] { phone }, Message = $"Here's your confirmation code: {dispatchToken}" }); } }
public async Task ConfirmEmail([FromUri] string email) { var user = DB.Users.Include(x => x.Tokens).FirstOrDefault(x => x.Id == UserId); if (!DB.Users.Any(x => x.Email == email)) { string dispatchToken = null; var existingToken = user.Tokens.FirstOrDefault(x => x.TokenType == TokenType.ChangeEmail); if (existingToken == null) { // Generate and send confirmation code to the phone number dispatchToken = UserTokensHelper.Generate(); UserToken dbToken = new UserToken() { TokenType = TokenType.ChangeEmail, User = user, Token = dispatchToken, }; DB.Tokens.Add(dbToken); // Save token DB.SaveChanges(); } else { dispatchToken = existingToken.Token; } // Dispatch token to user via Email await emailService.SendAsync(new SendMailOptions() { Subject = "Confirm Email", Destinations = new string[] { email }, Message = $"Here's your confirmation code: {dispatchToken}" }); } }
public async Task <IHttpActionResult> Register([FromUri] string accountType, [FromBody] RegisterModel model) { if (!(string.Equals(accountType, UserRoles.Admin, StringComparison.OrdinalIgnoreCase) || string.Equals(accountType, UserRoles.User, StringComparison.OrdinalIgnoreCase))) { ModelState.AddModelError("accountType", "Invalid account type"); return(BadRequest(ModelState)); } if (DB.Users.Any(x => x.Email == model.Email)) { return(BadRequest("Email already used!")); } if (DB.Users.Any(x => x.Username == model.Username)) { return(BadRequest("Username already exists!")); } if (DB.Users.Any(x => x.Phone == model.Phone)) { return(BadRequest("Phone No. already exists!")); } // var user = AppUser.New(model.Password); user.FullName = $"{model.FirstName} {model.LastName}".Trim(); user.Email = model.Email; user.Phone = model.Phone; user.Username = model.Username; user.EmailConfirmed = false; user.PhoneConfirmed = false; user.LockedOut = false; IEnumerable <Claim> claims = null; if (accountType.Equals(UserRoles.Admin, StringComparison.OrdinalIgnoreCase)) { claims = AppUser.GenerateAdminClaims(user); } else if (accountType.Equals(UserRoles.User, StringComparison.OrdinalIgnoreCase)) { claims = AppUser.GenerateUserClaims(user); } foreach (var claim in claims) { user.Claims.Add(new UserClaim() { ClaimType = claim.Type, Value = claim.Value, User = user }); } var activationToken = new UserToken() { TokenType = TokenType.ActivateAccount, User = user, Token = UserTokensHelper.Generate(), }; user.Tokens.Add(activationToken); // DB.Users.Add(user); // await DB.SaveChangesAsync(); // Phone Number Confirmation await smsService.SendAsync(new SendSMSOptions() { Destinations = new string[] { user.Phone }, Message = $"Here's your activation code: {activationToken.Token}", }); //else //{ // // send confirmation email // await emailService.SendAsync(new SendMailOptions() // { // Destinations = new string[] { user.Email }, // IsHtml = false, // Subject = "Activate Account", // Message = $"Hello {model.FirstName.Trim()}, here's your account activation code {activationToken.Token}" // }); //} // var response = Request.CreateResponse(HttpStatusCode.Created, Map <UserInfo>(user)); response.Headers.Add("UserToken", JwtHelper.SignToken(user, audience: TokenAudiences.ActivateAccount)); return(ResponseMessage(response)); }