Exemplo n.º 1
0
        public HttpResponseMessage Authenticate([FromBody] SystemUser user)
        {
            if (user == null)
            {
                var ex = new ArgumentNullException("user");
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }

            if ((user = SystemUser.Authenticate(user.Username, user.Password)) != null)
            {
                var authUser = new SystemUser
                {
                    Id       = user.Id,
                    Username = user.Username,
                };
                authUser.GenerateAuthToken();
                ApplicationContext.AddAuthenticatedUser(authUser);

                return(Request.CreateResponse(HttpStatusCode.OK, authUser));
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }
        }
Exemplo n.º 2
0
        public async Task <HttpResponseMessage> Post([FromBody] SystemUser user)
        {
            try
            {
                if (SystemUser.UserExists(user.Username))
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, new { message = "Username already used" }));
                }

                user.Password = SystemUser.Base64Encode(user.Password);
                var result = await Model.SystemUser.Insert(user);

                result.GenerateAuthToken();
                ApplicationContext.AddAuthenticatedUser(result);
                return(Request.CreateResponse(HttpStatusCode.Created, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, ex));
            }
        }