public HttpResponseMessage RegisterUser(UserRegisterModel user)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.CreateUser(user);
                string nickname;
                int id;
                string rank;
                UserLoginModel loginUser = new UserLoginModel()
                {
                    Username = user.Username,
                    AuthKey = user.AuthKey
                };

                var sessionKey = UserDataPersister.LoginUser(loginUser, out nickname, out id,out rank);

                return new UserLoggedModel()
                {
                    Id = id,
                    Nickname = nickname,
                    SessionKey = sessionKey
                };
            });

            return responseMsg;
        }
        public HttpResponseMessage LoginUser(UserLoginModel user)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                string nickname;
                int id;
                string rank;
                var sessionKey = UserDataPersister.LoginUser(user, out nickname, out id,out rank);
                return new UserLoggedModel()
                {
                    Nickname = nickname,
                    SessionKey = sessionKey,
                    Rank=rank
                };
            });

            return responseMsg;
        }
        public static string LoginUser(UserLoginModel usr, out string nickname, out int id,out string rank)
        {
            ValidateUsername(usr.Username);
            ValidateAuthKey(usr.AuthKey);
            var context = new CoolBlogEntity();
            using (context)
            {
                var usernameToLower = usr.Username.ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username == usernameToLower && u.AuthKey== usr.AuthKey);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid username or password", "ERR_INV_USR");
                }

                var sessionKey = GenerateSessionKey(user.Id);
                user.SessionKey = sessionKey;
                nickname = user.Nickname;
                id = user.Id;
                rank = user.Rank.Name;
                context.SaveChanges();

                return sessionKey;
            }
        }