public override bool ChangePassword(string username, string oldPassword, string newPassword)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(oldPassword))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(newPassword))
     {
         return(false);
     }
     using (PMToolContext Context = new PMToolContext())
     {
         UserProfile User = null;
         User = Context.UserProfiles.FirstOrDefault(Usr => Usr.UserName == username);
         if (User == null)
         {
             return(false);
         }
         String  HashedPassword        = User.Password;
         Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, oldPassword));
         if (VerificationSucceeded)
         {
             User.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int Failures = User.PasswordFailuresSinceLastSuccess;
             if (Failures < MaxInvalidPasswordAttempts)
             {
                 User.PasswordFailuresSinceLastSuccess += 1;
                 User.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             else if (Failures >= MaxInvalidPasswordAttempts)
             {
                 User.LastPasswordFailureDate = DateTime.UtcNow;
                 User.LastLockoutDate         = DateTime.UtcNow;
                 User.IsLockedOut             = true;
             }
             Context.SaveChanges();
             return(false);
         }
         String NewHashedPassword = Crypto.HashPassword(newPassword);
         if (NewHashedPassword.Length > 128)
         {
             return(false);
         }
         User.Password = NewHashedPassword;
         User.LastPasswordChangedDate = DateTime.UtcNow;
         Context.SaveChanges();
         return(true);
     }
 }
Exemplo n.º 2
0
        public ActionResult Create(Comment comment)
        {
            if (ModelState.IsValid)
            {
                context.Comments.Add(comment);
                context.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.PossibleTasks = context.Tasks;
            return(View(comment));
        }
    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        if (providerUserKey is Guid)
        {
        }
        else
        {
            return(null);
        }

        using (PMToolContext Context = new PMToolContext())
        {
            UserProfile User = null;
            User = Context.UserProfiles.Find(providerUserKey);
            if (User != null)
            {
                if (userIsOnline)
                {
                    User.LastActivityDate = DateTime.UtcNow;
                    Context.SaveChanges();
                }
                return(new MembershipUser(Membership.Provider.Name, User.UserName, User.UserId, User.Email, null, null, User.IsApproved, User.IsLockedOut, User.CreateDate.Value, User.LastLoginDate.Value, User.LastActivityDate.Value, User.LastPasswordChangedDate.Value, User.LastLockoutDate.Value));
            }
            else
            {
                return(null);
            }
        }
    }
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(password))
     {
         return(false);
     }
     using (PMToolContext Context = new PMToolContext())
     {
         UserProfile User = null;
         User = Context.UserProfiles.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return(false);
         }
         //if (!User.IsApproved)
         //{
         //    return false;
         //}
         //if (User.IsLockedOut)
         //{
         //    return false;
         //}
         String  HashedPassword        = User.Password;
         Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, password));
         if (VerificationSucceeded)
         {
             User.PasswordFailuresSinceLastSuccess = 0;
             User.LastLoginDate    = DateTime.UtcNow;
             User.LastActivityDate = DateTime.UtcNow;
         }
         else
         {
             int Failures = User.PasswordFailuresSinceLastSuccess;
             if (Failures < MaxInvalidPasswordAttempts)
             {
                 User.PasswordFailuresSinceLastSuccess += 1;
                 User.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             else if (Failures >= MaxInvalidPasswordAttempts)
             {
                 User.LastPasswordFailureDate = DateTime.UtcNow;
                 User.LastLockoutDate         = DateTime.UtcNow;
                 User.IsLockedOut             = true;
             }
         }
         Context.SaveChanges();
         if (VerificationSucceeded)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
 public override MembershipUser GetUser(string username, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(null);
     }
     using (PMToolContext Context = new PMToolContext())
     {
         UserProfile User = null;
         User = Context.UserProfiles.FirstOrDefault(Usr => Usr.UserName == username);
         if (User != null)
         {
             if (userIsOnline)
             {
                 //User.LastActivityDate = DateTime.UtcNow;
                 Context.SaveChanges();
             }
             return(new MembershipUser(Membership.Provider.Name, User.UserName, User.UserId, User.Email, null, null, User.IsApproved, User.IsLockedOut, User.CreateDate.Value, User.LastLoginDate.Value, User.LastActivityDate.Value, User.LastPasswordChangedDate.Value, User.LastLockoutDate.Value));
         }
         else
         {
             return(null);
         }
     }
 }
Exemplo n.º 6
0
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return(false);
     }
     using (PMToolContext Context = new PMToolContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return(false);
         }
         if (throwOnPopulatedRole)
         {
             if (Role.Users.Any())
             {
                 return(false);
             }
         }
         else
         {
             Role.Users.Clear();
         }
         Context.Roles.Remove(Role);
         Context.SaveChanges();
         return(true);
     }
 }
    public string CreateAccount(string userName, string password, bool requireConfirmationToken)
    {
        if (string.IsNullOrEmpty(userName))
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
        }

        if (string.IsNullOrEmpty(password))
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
        }

        string hashedPassword = Crypto.HashPassword(password);

        if (hashedPassword.Length > 128)
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
        }

        using (PMToolContext Context = new PMToolContext())
        {
            if (Context.UserProfiles.Where(Usr => Usr.UserName == userName).Any())
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
            }

            string token = string.Empty;
            if (requireConfirmationToken)
            {
                token = GenerateToken();
            }

            UserProfile NewUser = new UserProfile
            {
                //UserId = Guid.NewGuid(),
                UserName = userName,
                //Password = hashedPassword,
                //IsApproved = !requireConfirmationToken,
                Email = string.Empty,
                //CreateDate = DateTime.UtcNow,
                //LastPasswordChangedDate = DateTime.UtcNow,
                //PasswordFailuresSinceLastSuccess = 0,
                //LastLoginDate = DateTime.UtcNow,
                //LastActivityDate = DateTime.UtcNow,
                //LastLockoutDate = DateTime.UtcNow,
                //IsLockedOut = false,
                //LastPasswordFailureDate = DateTime.UtcNow,
                //ConfirmationToken = token
            };

            Context.UserProfiles.Add(NewUser);
            Context.SaveChanges();
            return(token);
        }
    }
 public override bool UnlockUser(string userName)
 {
     using (PMToolContext Context = new PMToolContext())
     {
         UserProfile User = null;
         User = Context.UserProfiles.FirstOrDefault(Usr => Usr.Username == userName);
         if (User != null)
         {
             User.IsLockedOut = false;
             User.PasswordFailuresSinceLastSuccess = 0;
             Context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Exemplo n.º 9
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (PMToolContext Context = new PMToolContext())
     {
         List <UserProfile> Users = Context.UserProfiles.Where(Usr => usernames.Contains(Usr.Username)).ToList();
         List <Role>        Roles = Context.Roles.Where(Rl => roleNames.Contains(Rl.RoleName)).ToList();
         foreach (UserProfile user in Users)
         {
             foreach (Role role in Roles)
             {
                 if (!user.Roles.Contains(role))
                 {
                     user.Roles.Add(role);
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Exemplo n.º 10
0
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (PMToolContext Context = new PMToolContext())
         {
             Role Role = null;
             Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
             if (Role == null)
             {
                 Role NewRole = new Role
                 {
                     RoleId   = Guid.NewGuid(),
                     RoleName = roleName
                 };
                 Context.Roles.Add(NewRole);
                 Context.SaveChanges();
             }
         }
     }
 }
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     using (PMToolContext Context = new PMToolContext())
     {
         UserProfile User = null;
         User = Context.UserProfiles.FirstOrDefault(Usr => Usr.UserName == username);
         if (User != null)
         {
             Context.UserProfiles.Remove(User);
             Context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Exemplo n.º 12
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (PMToolContext Context = new PMToolContext())
     {
         foreach (String username in usernames)
         {
             String      us   = username;
             UserProfile user = Context.UserProfiles.FirstOrDefault(U => U.UserName == us);
             if (user != null)
             {
                 foreach (String roleName in roleNames)
                 {
                     String rl   = roleName;
                     Role   role = user.Roles.FirstOrDefault(R => R.RoleName == rl);
                     if (role != null)
                     {
                         user.Roles.Remove(role);
                     }
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Exemplo n.º 13
0
 public void Save()
 {
     context.SaveChanges();
 }