Ejemplo n.º 1
0
        // POST api/TodoList
        public HttpResponseMessage PostTodoItem(TodoItem todoitem)
        {
            if (ModelState.IsValid)
            {
                var user = _db.Users.Single(x => x.Username == HttpContext.Current.User.Identity.Name);

                todoitem.UserId = user.Id;

                _db.TodoItems.Add(todoitem);
                _db.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.Created, todoitem));
            }

            return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                          new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) }));
        }
Ejemplo n.º 2
0
        public HttpResponseMessage Login(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                if (WebSecurity.Login(model.Username, model.Password, model.RememberMe))
                {
                    FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);

                    return(Request.CreateResponse(HttpStatusCode.OK));
                }

                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }

            // If we got this far, something failed
            return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                          new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) }));
        }
Ejemplo n.º 3
0
        // PUT api/TodoList/
        public HttpResponseMessage PutTodoItem(TodoItem todoitem)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                              new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) }));
            }

            _db.Entry(todoitem).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Ejemplo n.º 4
0
        //[ValidateAntiForgeryToken]
        public HttpResponseMessage Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.Username, model.Password);
                    WebSecurity.Login(model.Username, model.Password);

                    FormsAuthentication.SetAuthCookie(model.Username, createPersistentCookie: false);
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed
            return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                          new { errors = ModelStateHelpers.GetErrorsFromModelState(ModelState) }));
        }