예제 #1
0
 public bool UpdatePassword(PasswordUpdate passwordUpdate)
 {
     return _repo.UpdatePassword(passwordUpdate);
 }
        public bool UpdatePassword(PasswordUpdate newPassword)
        {
            try {
                var userRequest = _ctx.UserPasswordRequests.Single(x => x.PasswordToken == newPassword.Token);
                if(userRequest != null)
                {
                    if (DateTime.Compare(DateTime.Now, userRequest.ExpirationDate) <= 0) {

                        var updateP = _ctx.Users.Single(x => x.UserName == userRequest.UserName);
                        if(updateP != null)
                        {
                            using (MD5 md5Hash = MD5.Create())
                            {
                                updateP.Password = HelperClass.GetMd5Hash(md5Hash, newPassword.Password + updateP.UserGuid.ToString());
                            }
                            userRequest.ExpirationDate = DateTime.Now.AddDays(-1);
                            if (_ctx.SaveChanges() > 0) {
                                return true;

                            }
                            else
                            {
                                var res = new HttpResponseMessage(HttpStatusCode.BadRequest)
                                {
                                    Content = new StringContent(string.Format("Something went wrong. DB.")),
                                    ReasonPhrase = "Something went wrong. DB."
                                };
                                throw new HttpResponseException(res);
                            }
                        }
                        else
                        {
                            var res = new HttpResponseMessage(HttpStatusCode.BadRequest)
                            {
                                Content = new StringContent(string.Format("Something went wrong. DB.")),
                                ReasonPhrase = "Something went wrong. DB."
                            };
                            throw new HttpResponseException(res);
                        }
                    }
                    else {
                        var res = new HttpResponseMessage(HttpStatusCode.RequestTimeout)
                        {
                            Content = new StringContent(string.Format("Something went wrong. Timeout.")),
                            ReasonPhrase = "Something went wrong. Timeout."
                        };
                        throw new HttpResponseException(res);
                    }
                }
                else
                {
                    var res = new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent(string.Format("Something went wrong. Timeout.")),
                        ReasonPhrase = "Something went wrong. Timeout."
                    };
                    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. DB.")),
                    ReasonPhrase = "Something went wrong. DB."
                };
                throw new HttpResponseException(res);
            }
        }
 public bool UpdatePassword(PasswordUpdate newPassword)
 {
     try {
         var userRequest = _ctx.UserPasswordRequests.Single(x => x.PasswordToken == newPassword.Token);
         if (userRequest != null)
         {
             if (DateTime.Compare(DateTime.Now, userRequest.ExpirationDate) <= 0)
             {
                 var updateP = _ctx.Users.Single(x => x.UserName == userRequest.UserName);
                 if (updateP != null)
                 {
                     using (MD5 md5Hash = MD5.Create())
                     {
                         updateP.Password = HelperClass.GetMd5Hash(md5Hash, newPassword.Password + updateP.UserGuid.ToString());
                     }
                     userRequest.ExpirationDate = DateTime.Now.AddDays(-1);
                     if (_ctx.SaveChanges() > 0)
                     {
                         return(true);
                     }
                     else
                     {
                         var res = new HttpResponseMessage(HttpStatusCode.BadRequest)
                         {
                             Content      = new StringContent(string.Format("Something went wrong. DB.")),
                             ReasonPhrase = "Something went wrong. DB."
                         };
                         throw new HttpResponseException(res);
                     }
                 }
                 else
                 {
                     var res = new HttpResponseMessage(HttpStatusCode.BadRequest)
                     {
                         Content      = new StringContent(string.Format("Something went wrong. DB.")),
                         ReasonPhrase = "Something went wrong. DB."
                     };
                     throw new HttpResponseException(res);
                 }
             }
             else
             {
                 var res = new HttpResponseMessage(HttpStatusCode.RequestTimeout)
                 {
                     Content      = new StringContent(string.Format("Something went wrong. Timeout.")),
                     ReasonPhrase = "Something went wrong. Timeout."
                 };
                 throw new HttpResponseException(res);
             }
         }
         else
         {
             var res = new HttpResponseMessage(HttpStatusCode.BadRequest)
             {
                 Content      = new StringContent(string.Format("Something went wrong. Timeout.")),
                 ReasonPhrase = "Something went wrong. Timeout."
             };
             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. DB.")),
             ReasonPhrase = "Something went wrong. DB."
         };
         throw new HttpResponseException(res);
     }
 }