コード例 #1
0
        public UserModel GetUserInfo(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new dbf609f467420e40209014a26e008b568aEntities();

                var user = context.Users.FirstOrDefault(
                    usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid sessionKey");
                }

                var model = new UserModel()
                {
                    Name = user.Name,
                    Username = user.Username,
                    Password = user.Password
                };

                return model;
            });

            return responseMsg;
        }
コード例 #2
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new dbf609f467420e40209014a26e008b568aEntities();
                  using (context)
                  {
                      this.ValidateUsername(model.Username);
                      this.ValidatePassword(model.Password);
                      var usernameToLower = model.Username.ToLower();
                      var user = context.Users.FirstOrDefault(
                          usr => usr.Username == usernameToLower
                          && usr.Password == model.Password);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid username or password");
                      }

                      if (user.SessionKey == null)
                      {
                          user.SessionKey = user.Id.ToString();
                          context.SaveChanges();
                      }

                      var loggedModel = new UserLoggedModel()
                      {
                          Name = user.Name,
                          SessionKey = user.SessionKey
                      };

                      var response =
                          this.Request.CreateResponse(HttpStatusCode.Created,
                                          loggedModel);
                      return response;
                  }
              });

            return responseMsg;
        }
コード例 #3
0
        public HttpResponseMessage PutLogoutUser(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  var context = new dbf609f467420e40209014a26e008b568aEntities();
                  using (context)
                  {
                      var user = context.Users.FirstOrDefault(
                          usr => usr.SessionKey == sessionKey);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid sessionKey");
                      }

                      user.SessionKey = null;
                      context.SaveChanges();

                      var response =
                          this.Request.CreateResponse(HttpStatusCode.OK);
                      return response;
                  }
              });

            return responseMsg;
        }