コード例 #1
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (RMWContext Context = new RMWContext())
     {
         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
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (RMWContext Context = new RMWContext())
     {
         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();
     }
 }
コード例 #3
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (RMWContext Context = new RMWContext())
         {
             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();
             }
         }
     }
 }
コード例 #4
0
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<RMWContext>(null);

                try
                {
                    using (var context = new RMWContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("RMWContext", "Users", "Id", "UserName", autoCreateTables: false);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
コード例 #5
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return null;
     }
     using (RMWContext Context = new RMWContext())
     {
         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;
         }
     }
 }
コード例 #6
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override string[] GetRolesForUser(string username)
 {
     if (string.IsNullOrEmpty(username))
     {
         return null;
     }
     using (RMWContext Context = new RMWContext())
     {
         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;
         }
     }
 }
コード例 #7
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override string[] GetAllRoles()
 {
     using (RMWContext Context = new RMWContext())
     {
         return Context.Roles.Select(Rl => Rl.RoleName).ToArray();
     }
 }
コード例 #8
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            if (string.IsNullOrEmpty(roleName))
            {
                return null;
            }

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

            using (RMWContext Context = new RMWContext())
            {

                return (from Rl in Context.Roles from Usr in Rl.Users where Rl.RoleName == roleName && Usr.Username.Contains(usernameToMatch) select Usr.Username).ToArray();
            }
        }
コード例 #9
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 (var Context = new RMWContext())
            {
                if (Context.Users.Any(Usr => Usr.Username == username))
                {
                    status = MembershipCreateStatus.DuplicateUserName;
                    return null;
                }

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

                var NewUser = new User
                {
                    //Id = 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(System.Web.Security.Membership.Provider.Name, NewUser.Username, NewUser.Id, NewUser.Email, null, null, NewUser.IsApproved, NewUser.IsLockedOut, NewUser.CreateDate.Value, NewUser.LastLoginDate.Value, NewUser.LastActivityDate.Value, NewUser.LastPasswordChangedDate.Value, NewUser.LastLockoutDate.Value);
            }
        }
コード例 #10
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override bool RoleExists(string roleName)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (RMWContext Context = new RMWContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role != null)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 }
コード例 #11
0
 public override bool UnlockUser(string userName)
 {
     using (RMWContext Context = new RMWContext())
     {
         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;
         }
     }
 }
コード例 #12
0
 public override string GetUserNameByEmail(string email)
 {
     using (RMWContext Context = new RMWContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Email == email);
         if (User != null)
         {
             return User.Username;
         }
         else
         {
             return string.Empty;
         }
     }
 }
コード例 #13
0
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            if (providerUserKey is Guid) { }
            else
            {
                return null;
            }

            using (var Context = new RMWContext())
            {
                User user = null;
                user = Context.Users.Find(providerUserKey);
                if (user == null)
                {
                    return null;
                }
                if (userIsOnline)
                {
                    user.LastActivityDate = DateTime.UtcNow;
                    Context.SaveChanges();
                }
                return new MembershipUser(System.Web.Security.Membership.Provider.Name, user.Username, user.Id,
                                          user.Email, null, null, user.IsApproved, user.IsLockedOut,
                                          user.CreateDate.Value, user.LastLoginDate.Value,
                                          user.LastActivityDate.Value, user.LastPasswordChangedDate.Value,
                                          user.LastLockoutDate.Value);
            }
        }
コード例 #14
0
 public override MembershipUser GetUser(string username, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(username))
     {
         return null;
     }
     using (var Context = new RMWContext())
     {
         User user = null;
         user = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (user == null)
         {
             return null;
         }
         if (userIsOnline)
         {
             user.LastActivityDate = DateTime.UtcNow;
             Context.SaveChanges();
         }
         return new MembershipUser(System.Web.Security.Membership.Provider.Name, user.Username, user.Id,
                                   user.Email, null, null, user.IsApproved, user.IsLockedOut,
                                   user.CreateDate.Value, user.LastLoginDate.Value,
                                   user.LastActivityDate.Value, user.LastPasswordChangedDate.Value,
                                   user.LastLockoutDate.Value);
     }
 }
コード例 #15
0
 public override int GetNumberOfUsersOnline()
 {
     var dateActive = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(Convert.ToDouble(System.Web.Security.Membership.UserIsOnlineTimeWindow)));
     using (var context = new RMWContext())
     {
         return context.Users.Count(usr => usr.LastActivityDate > dateActive);
     }
 }
コード例 #16
0
 public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
 {
     MembershipUserCollection MembershipUsers = new MembershipUserCollection();
     using (RMWContext Context = new RMWContext())
     {
         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(System.Web.Security.Membership.Provider.Name, user.Username, user.Id, 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;
 }
コード例 #17
0
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     using (RMWContext Context = new RMWContext())
     {
         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;
         }
     }
 }
コード例 #18
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override bool IsUserInRole(string username, string roleName)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     if (string.IsNullOrEmpty(roleName))
     {
         return false;
     }
     using (RMWContext Context = new RMWContext())
     {
         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);
     }
 }
コード例 #19
0
ファイル: CodeFirstRoleProvider.cs プロジェクト: ryn0/home
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (RMWContext Context = new RMWContext())
     {
         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();
     }
 }
コード例 #20
0
ファイル: AccountController.cs プロジェクト: ryn0/home
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated ||
                !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (var db = new RMWContext())
                {
                    var user = db.Users.FirstOrDefault(u => u.Username.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.Users.Add(new User {Username = model.UserName});
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName",
                                                 "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
コード例 #21
0
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(username))
     {
         return false;
     }
     if (string.IsNullOrEmpty(password))
     {
         return false;
     }
     using (var Context = new RMWContext())
     {
         User user = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (user == null)
         {
             return false;
         }
         if (!user.IsApproved)
         {
             return false;
         }
         if (user.IsLockedOut)
         {
             return false;
         }
         var hashedPassword = user.Password;
         var 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();
         return verificationSucceeded;
     }
 }
コード例 #22
0
        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 (RMWContext Context = new RMWContext())
            {
                if (Context.Users.Where(Usr => Usr.Username == userName).Any())
                {
                    throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
                }

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

                User NewUser = new User
                {
                    //Id = 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.Users.Add(NewUser);
                Context.SaveChanges();
                return token;
            }
        }
コード例 #23
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 (var Context = new RMWContext())
     {
         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;
     }
 }
コード例 #24
0
ファイル: WebSecurity.cs プロジェクト: ryn0/home
        public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName)
        {
            MembershipCreateStatus CreateStatus;
            System.Web.Security.Membership.CreateUser(Username, Password, Email, null, null, IsApproved, Guid.NewGuid(), out CreateStatus);

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

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

            return CreateStatus;
        }