Example #1
0
        public IHttpActionResult PostUser(User user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Helpers.SecurityHelper _securityHelper = new Helpers.SecurityHelper();
            user.UserPassword = _securityHelper.Md5Encryption(user.UserPassword);
            _userService.CreateUser(user);
            _userService.SaveUser();
            return(CreatedAtRoute("DefaultApi", new { id = user.UserID }, user));
        }
Example #2
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //TODO: Add user check

            string json = string.Empty;

            Model.User User = _userService.UserLogin(context.UserName, _securityHelper.Md5Encryption(context.Password));
            if (User != null)
            {
                ClaimsIdentity oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
                oAuthIdentity.AddClaim(new Claim("RoleId", User.RoleID.ToString()));
                oAuthIdentity.AddClaim(new Claim("UserId", User.UserID.ToString()));
                AuthenticationProperties properties = CreateProperties("WebAPI");
                AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);
                context.Validated(ticket);
            }
            else
            {
                context.SetError("Invalid_request");
                return;
            }
        }
Example #3
0
        // PUT api/Users/5
        public IHttpActionResult PutUser(int id, User user)
        {
            user.Role = null;
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.UserID)
            {
                return(BadRequest());
            }

            try
            {
                Model.User _user = _userService.GetUser(id);
                if (user.UserPassword != _user.UserPassword)
                {
                    Helpers.SecurityHelper _securityHelper = new Helpers.SecurityHelper();
                    user.UserPassword = _securityHelper.Md5Encryption(user.UserPassword);
                }
                _userService.UpdateUser(user);
                _userService.SaveUser();
            }
            catch (Exception ex)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }