Exemple #1
0
 // POST: api/APN_User
 public HttpResponseMessage Post([FromBody] APN_User user)
 {
     try
     {
         users.SaveData(user);
         return(Request.CreateResponse(HttpStatusCode.Created, user.FirstName));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
 public ActionResult Login(APN_User user)
 {
     if (IsValid(user.Email, user.Password))
     {
         FormsAuthentication.SetAuthCookie(user.Email, false);
         return(RedirectToAction("Vehicle", "Ads"));
     }
     else
     {
         ModelState.AddModelError("", "Login details are wrong.");
     }
     return(View(user));
 }
        public ActionResult Register(APN_User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    APN_User newUser    = new APN_User();
                    var      crypto     = new SimpleCrypto.PBKDF2();
                    var      encrypPass = crypto.Compute(user.Password);
                    newUser.Email        = user.Email;
                    newUser.Password     = encrypPass;
                    newUser.PasswordSalt = crypto.Salt;
                    newUser.FirstName    = user.FirstName;
                    newUser.UserType     = "User";
                    newUser.CreatedDate  = DateTime.Now;
                    newUser.IsActive     = true;

                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(String.Concat(baseUrl, "api/APN_User"));
                        var resposeTask = client.PostAsJsonAsync <APN_User>("APN_User", newUser);
                        resposeTask.Wait();
                        if (resposeTask.Result.IsSuccessStatusCode)
                        {
                            return(RedirectToAction("Login", "Account"));
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            return(View());
        }
Exemple #4
0
 // PUT: api/APN_User/5
 public HttpResponseMessage Put(int id, [FromBody] APN_User user)
 {
     try
     {
         var entity = users.GetUniqueData(id);
         if (entity == null)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "User with id " + id + " not found to update"));
         }
         else
         {
             users.UpdateRecord(id, user);
             return(Request.CreateResponse(HttpStatusCode.OK, "User with id " + id + " update successfully.."));
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }