protected override void InternalProcessRequest(HttpContext context)
        {
            Reminder2DataContext dataCtx = new Reminder2DataContext();
            var query = from Task t in dataCtx.Tasks
                        where t.idTask == TaskID
                        select new
                        {
                            Task = t,
                            UserName = t.User.UserName
                        };

            var result = query.FirstOrDefault();
            if (result != null)
                try
                {
                    if (result.UserName.Equals(context.User.Identity.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        result.Task.Checked = true;
                        dataCtx.SubmitChanges();
                        WriteOutputStatus(CommonStatusCode.OK, "OK.");
                    }
                    else
                        WriteOutputStatus(CommonStatusCode.Forbidden, "You are not allowed to mark this task as checked.");
                }
                catch (Exception ex)
                {
                    WriteOutputStatus(CommonStatusCode.ServerError, string.Format("Couldn't mark the task as checked. {0}", ex.Message));
                }
            else
                WriteOutputStatus(CommonStatusCode.NotFound, "Task not found.");
        }
        /// <summary>
        /// Processes the HTTP request.
        /// </summary>
        /// <param name="context">Context of the HTTP operation.</param>
        protected override void InternalProcessRequest(HttpContext context)
        {
            Reminder2DataContext dataCtx = new Reminder2DataContext();
            var query = from User u in dataCtx.Users
                        where u.UserName == context.User.Identity.Name
                        select u.idUser;
            if (query.Count() > 0)
                try
                {
                    Task task = new Task()
                    {
                        Message = this.Message,
                        DateTime = this.DateTime,
                        idUser = query.First()
                    };
                    dataCtx.Tasks.InsertOnSubmit(task);
                    dataCtx.SubmitChanges();
                    WriteOutputStatus(200, "OK.");

                    XmlOutput.WriteStartElement("task");
                    try
                    {
                        XmlOutput.WriteAttributeString("id", task.idTask.ToString());
                    }
                    finally
                    {
                        XmlOutput.WriteEndElement();
                    }
                }
                catch (Exception ex)
                {
                    WriteOutputStatus(CommonStatusCode.ServerError, string.Format("Couldn't add task. {0}", ex.Message));
                }
            else
                WriteOutputStatus(CommonStatusCode.ServerError, "Invalid user Id.");
        }
        protected override void InternalProcessRequest(HttpContext context)
        {
            Reminder2DataContext ctx = new Reminder2DataContext();
            try
            {
                IQueryable<Task> tasks = null;
                DateTime utcNow = DateTime.UtcNow;
                switch (RequestType)
                {
                    case TasksRequestType.Recent:
                        //select tasks that are either less than 2 days old or not checked
                        tasks = from Task t in ctx.Tasks
                                where t.User.UserName == context.User.Identity.Name &&
                                      !t.Deleted &&
                                      (utcNow - t.DateTime < new TimeSpan(2, 0, 0, 0) || !t.Checked)
                                select t;
                        break;

                    case TasksRequestType.Unchecked:
                        //select all unchecked tasks
                        tasks = from Task t in ctx.Tasks
                                where t.User.UserName == context.User.Identity.Name &&
                                      !t.Deleted &&
                                      !t.Checked
                                select t;
                        break;
                }

                if (tasks != null)
                    WriteTasks(tasks);
            }
            catch (Exception ex)
            {
                WriteOutputStatus(CommonStatusCode.ServerError, string.Format("Could not retrieve tasks. {0}", ex.Message));
            }
        }
        /// <summary>
        /// Changes password for an user.
        /// </summary>
        /// <param name="username">Name of the user.</param>
        /// <param name="oldPassword">Old password.</param>
        /// <param name="newPassword">New password.</param>
        /// <returns>True if the operation was successful, false otherwise.</returns>
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            Reminder2DataContext ctx = new Reminder2DataContext();
            var query = from User u in ctx.Users
                        where (u.UserName == username) &&
                              (u.Password == oldPassword)
                        select u;

            User found = query.FirstOrDefault();
            if (found == null)
                return false;
            try
            {
                found.Password = newPassword;
                ctx.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
 /// <summary>
 /// Validates an user against the database.
 /// </summary>
 /// <param name="username">User name.</param>
 /// <param name="password">User password.</param>
 /// <returns>True if the user was succesfully validated, false otherwise.</returns>
 public override bool ValidateUser(string username, string password)
 {
     Reminder2DataContext ctx = new Reminder2DataContext();
     var query = from User u in ctx.Users
                 where (u.UserName == username) &&
                       (u.Password == password)
                 select u;
     return query.FirstOrDefault() != null;
 }
        /// <summary>
        /// Updates an user's data in the database.
        /// </summary>
        /// <param name="user">The user to be updated.</param>
        public override void UpdateUser(MembershipUser user)
        {
            Rem2MembershipUser memUser = user as Rem2MembershipUser;
            if (memUser == null)
                throw new ArgumentException("The supplied Membership user is not compatible with the DB.");

            Reminder2DataContext ctx = new Reminder2DataContext();
            var query = from User u in ctx.Users
                        where u.UserName == user.UserName
                        select u;
            User found = query.FirstOrDefault();
            if (found == null)
                return;

            found.Email = memUser.Email;
            found.FirstName = memUser.FirstName;
            found.LastName = memUser.LastName;
            ctx.SubmitChanges();
        }
 /// <summary>
 /// Gats an user's name from the database given his e-mail address.
 /// </summary>
 /// <param name="email"></param>
 /// <returns></returns>
 public override string GetUserNameByEmail(string email)
 {
     Reminder2DataContext ctx = new Reminder2DataContext();
     var query = from User u in ctx.Users
                 where u.Email == email
                 select u.UserName;
     return query.FirstOrDefault();
 }
        /// <summary>
        /// Gets a MembershipUser from the database given his database ID.
        /// </summary>
        /// <param name="providerUserKey">ID of the user in the DB.</param>
        /// <param name="userIsOnline"></param>
        /// <returns>The fetched MembershipUser.</returns>
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            if (!(providerUserKey is int))
                return null;

            Reminder2DataContext ctx = new Reminder2DataContext();
            var query = from User u in ctx.Users
                        where u.idUser == (int)providerUserKey
                        select u;

            User found = query.FirstOrDefault();
            if (found != null)
                return new Rem2MembershipUser(this.Name, found);
            else
                return null;
        }
        /// <summary>
        /// Gets an MembershipUser from the database, given his username.
        /// </summary>
        /// <param name="username">User name.</param>
        /// <param name="userIsOnline"></param>
        /// <returns>The fetched MemebershipUser.</returns>
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            Reminder2DataContext ctx = new Reminder2DataContext();
            var query = from User u in ctx.Users
                        where u.UserName == username
                        select u;

            User found = query.FirstOrDefault();
            if (found != null)
                return new Rem2MembershipUser(this.Name, found);
            else
                return null;
        }
 /// <summary>
 /// Gets an user's password from the database.
 /// </summary>
 /// <param name="username">Name of the user.</param>
 /// <param name="answer"></param>
 /// <returns>User's password.</returns>
 public override string GetPassword(string username, string answer)
 {
     Reminder2DataContext ctx = new Reminder2DataContext();
     var query = from User u in ctx.Users
                 where u.UserName == username
                 select u.Password;
     return query.FirstOrDefault();
 }
        /// <summary>
        /// Creates a new user and inserts him into the DB.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">User password.</param>
        /// <param name="email">E-mail address.</param>
        /// <param name="firstName">First name.</param>
        /// <param name="lastName">Last name.</param>
        /// <param name="status">User creation status.</param>
        /// <returns>A new Membership user if successful, null otherwise.</returns>
        public Rem2MembershipUser CreateUser(string userName, string password, string email,
            string firstName, string lastName, out MembershipCreateStatus status)
        {
            Reminder2DataContext ctx = new Reminder2DataContext();

            //check user name
            if (string.IsNullOrEmpty(userName))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }

            //check if the user already exists
            if (ctx.Users.FirstOrDefault(u => u.UserName == userName) != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }

            Rem2MembershipUser memUser = null;
            try
            {
                User dbUser = new User()
                {
                    UserName = userName,
                    Password = password,
                    Email = email,
                    FirstName = firstName,
                    LastName = lastName
                };
                ctx.Users.InsertOnSubmit(dbUser);
                ctx.SubmitChanges();
                memUser = new Rem2MembershipUser(this.Name, dbUser);
            }
            catch
            {
                status = MembershipCreateStatus.ProviderError;
                return null;
            }

            status = MembershipCreateStatus.Success;
            return memUser;
        }