Пример #1
0
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         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;
     }
 }
Пример #2
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            System.Data.Entity.Database.SetInitializer<DataContext>(new DataContextInitializer());

            using (DataContext TestContext = new DataContext())
            {
            TestContext.Users.Count();
            }
        }
Пример #3
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (DataContext Context = new DataContext())
     {
         List<User> Users = Context.Users.Where(Usr => usernames.Contains(Usr.Username)).ToList();
         List<Role> Roles = Context.Roles.Where(Rl => roleNames.Contains(Rl.RoleName)).ToList();
         foreach (User user in Users)
         {
             foreach (Role role in Roles)
             {
                 if (!user.Roles.Contains(role))
                 {
                     user.Roles.Add(role);
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Пример #4
0
        protected void Application_Start()
        {
            // Create DB
            Database.SetInitializer(new DataContextInitializer());

            // Connection to DB and Test
            using (var db = new DataContext())
            {
                var query = from post in db.Posts
                            select post;
                var list = query.ToList();
            }

            AreaRegistration.RegisterAllAreas();

            // Use LocalDB for Entity Framework by default
            Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
Пример #5
0
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (DataContext Context = new DataContext())
         {
             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();
             }
         }
     }
 }
Пример #6
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (DataContext Context = new DataContext())
     {
         foreach (String username in usernames)
         {
             String us = username;
             User user = Context.Users.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();
     }
 }
Пример #7
0
 public override bool IsUserInRole(string username, string roleName)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return false;
         }
         Role Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return false;
         }
         return User.Roles.Contains(Role);
     }
 }
Пример #8
0
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return null;
     }
     using (DataContext Context = new DataContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return Role.Users.Select(Usr => Usr.Username).ToArray();
         }
         else
         {
             return null;
         }
     }
 }
Пример #9
0
 public override string[] GetRolesForUser(string username)
 {
     if (string.IsNullOrEmpty(username))
     {
         return null;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             return User.Roles.Select(Rl => Rl.RoleName).ToArray();
         }
         else
         {
             return null;
         }
     }
 }
Пример #10
0
 public override string[] GetAllRoles()
 {
     using (DataContext Context = new DataContext())
     {
         return Context.Roles.Select(Rl => Rl.RoleName).ToArray();
     }
 }
Пример #11
0
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            if (string.IsNullOrEmpty(roleName))
            {
                return null;
            }

            if (string.IsNullOrEmpty(usernameToMatch))
            {
                return null;
            }

            using (DataContext Context = new DataContext())
            {

                return (from Rl in Context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username).ToArray();
            }
        }
Пример #12
0
 public override MembershipUser GetUser(string username, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(username))
     {
         return null;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.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;
         }
     }
 }
Пример #13
0
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     if (string.IsNullOrEmpty(password))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.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;
         }
     }
 }
Пример #14
0
 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 (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.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;
     }
 }
Пример #15
0
 public override bool UnlockUser(string userName)
 {
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (User != null)
         {
             User.IsLockedOut = false;
             User.PasswordFailuresSinceLastSuccess = 0;
             Context.SaveChanges();
             return true;
         }
         else
         {
             return false;
         }
     }
 }
Пример #16
0
 public override string GetUserNameByEmail(string email)
 {
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Email == email);
         if (User != null)
         {
             return User.Username;
         }
         else
         {
             return string.Empty;
         }
     }
 }
Пример #17
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            if (providerUserKey is Guid) { }
            else
            {
                return null;
            }

            using (DataContext Context = new DataContext())
            {
                User User = null;
                User = Context.Users.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;
                }
            }
        }
Пример #18
0
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 }
Пример #19
0
 public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
 {
     MembershipUserCollection MembershipUsers = new MembershipUserCollection();
     using (DataContext Context = new DataContext())
     {
         totalRecords = Context.Users.Count();
         IQueryable<User> Users = Context.Users.OrderBy(Usrn => Usrn.Username).Skip(pageIndex * pageSize).Take(pageSize);
         foreach (User user in Users)
         {
             MembershipUsers.Add(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));
         }
     }
     return MembershipUsers;
 }
Пример #20
0
        public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName)
        {
            MembershipCreateStatus CreateStatus;
            Membership.CreateUser(Username, Password, Email, null, null, IsApproved, null, out CreateStatus);

            if (CreateStatus == MembershipCreateStatus.Success)
            {
                using (DataContext Context = new DataContext())
                {
                    User User = Context.Users.FirstOrDefault(Usr => Usr.Username == Username);
                    User.FirstName = FirstName;
                    User.LastName = LastName;
                    Context.SaveChanges();
                }

                if (IsApproved)
                {
                    FormsAuthentication.SetAuthCookie(Username, false);
                }
            }

            return CreateStatus;
        }
Пример #21
0
 public override int GetNumberOfUsersOnline()
 {
     DateTime DateActive = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(Convert.ToDouble(Membership.UserIsOnlineTimeWindow)));
     using (DataContext Context = new DataContext())
     {
         return Context.Users.Where(Usr => Usr.LastActivityDate > DateActive).Count();
     }
 }
Пример #22
0
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            if (string.IsNullOrEmpty(username))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }
            if (string.IsNullOrEmpty(password))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }
            if (string.IsNullOrEmpty(email))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }

            string HashedPassword = Crypto.HashPassword(password);
            if (HashedPassword.Length > 128)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            using (DataContext Context = new DataContext())
            {
                if (Context.Users.Where(Usr => Usr.Username == username).Any())
                {
                    status = MembershipCreateStatus.DuplicateUserName;
                    return null;
                }

                if (Context.Users.Where(Usr => Usr.Email == email).Any())
                {
                    status = MembershipCreateStatus.DuplicateEmail;
                    return null;
                }

                User NewUser = new User
                {
                    UserId = Guid.NewGuid(),
                    Username = username,
                    Password = HashedPassword,
                    IsApproved = isApproved,
                    Email = email,
                    CreateDate = DateTime.UtcNow,
                    LastPasswordChangedDate = DateTime.UtcNow,
                    PasswordFailuresSinceLastSuccess = 0,
                    LastLoginDate = DateTime.UtcNow,
                    LastActivityDate = DateTime.UtcNow,
                    LastLockoutDate = DateTime.UtcNow,
                    IsLockedOut = false,
                    LastPasswordFailureDate = DateTime.UtcNow
                };

                Context.Users.Add(NewUser);
                Context.SaveChanges();
                status = MembershipCreateStatus.Success;
                return new MembershipUser(Membership.Provider.Name, NewUser.Username, NewUser.UserId, NewUser.Email, null, null, NewUser.IsApproved, NewUser.IsLockedOut, NewUser.CreateDate.Value, NewUser.LastLoginDate.Value, NewUser.LastActivityDate.Value, NewUser.LastPasswordChangedDate.Value, NewUser.LastLockoutDate.Value);
            }
        }
Пример #23
0
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     using (DataContext Context = new DataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             Context.Users.Remove(User);
             Context.SaveChanges();
             return true;
         }
         else
         {
             return false;
         }
     }
 }
Пример #24
0
        public ActionResult ResetPassword(RegisterModel model)
        {
            var db = new DataContext();
            var user = db.Users.FirstOrDefault(x => x.Email == model.Email);

            if(user != null)
            {
                var password = RandomPassword.Generate();
                var membership = new CodeFirstMembershipProvider();
                if (Request.Url != null)
                {
                    MailUtil.SendNewPassword(password, model.Email, Request.Url.Host + Url.Action("ChangePassword"), user.Username);
                    if(membership.ChangePasswordForce(user.Email, password))
                    {
                        return View("ResetSuccess", model);
                    }
                }
            }
            ViewBag.Fail = "Fant ikke noen bruker med den epost-adressen. Har du skrevet feil?";
            return View();
        }
 public bool ChangePasswordForce(string email, string newPassword)
 {
     using (var db = new DataContext())
     {
         var user = db.Users.FirstOrDefault(x => x.Email == email);
         if(user != null)
         {
             var newHashedPassword = Crypto.HashPassword(newPassword);
             if (newHashedPassword.Length > 128)
             {
                 return false;
             }
             user.Password = newHashedPassword;
             user.LastPasswordChangedDate = DateTime.UtcNow;
             db.SaveChanges();
             return true;
         }
     }
     return false;
 }