public bool SendPasswordResetLink(User existinguser) { try { User checkUser = _ctx.Users.Single(x => x.Email == existinguser.Email); if (checkUser != null) { byte[] token = new byte[10]; UserPasswordRequest req = new UserPasswordRequest(); using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) { rng.GetBytes(token); req.PasswordToken = HttpServerUtility.UrlTokenEncode(token); }; req.UserName = checkUser.UserName; req.ExpirationDate = DateTime.Now.AddMinutes(30); _ctx.UserPasswordRequests.Add(req); if (_ctx.SaveChanges() > 0) { return(true); } else { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } } else { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } } catch (InvalidOperationException) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } catch (DbUpdateException) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } }
public bool SendPasswordResetLink(User existinguser) { try { User checkUser = _ctx.Users.Single(x => x.Email == existinguser.Email); if (checkUser != null) { byte[] token = new byte[10]; UserPasswordRequest req = new UserPasswordRequest(); using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) { rng.GetBytes(token); req.PasswordToken = HttpServerUtility.UrlTokenEncode(token); }; req.UserName = checkUser.UserName; req.ExpirationDate = DateTime.Now.AddMinutes(30); _ctx.UserPasswordRequests.Add(req); if (_ctx.SaveChanges() > 0) return true; else { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } } else { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } } catch (InvalidOperationException) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } catch (DbUpdateException) { var res = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(string.Format("Something went wrong.")), ReasonPhrase = "Something went wrong." }; throw new HttpResponseException(res); } }