public void CreateRole(string roleName, int? parentRoleId = null)
 {
     Role newRole = new Role() { Name = roleName, ParentRoleId = parentRoleId};
     DataContext db = new DataContext();
     db.Roles.Add(newRole);
     db.SaveChanges();
 }
 public override void CreateRole(string roleName)
 {
     Role newRole = new Role() { Name = roleName};
     DataContext db = new DataContext();
     db.Roles.Add(newRole);
     db.SaveChanges();
 }
        public MembershipUser CreateUser(string email, string password)
        {
            MembershipUser membershipUser = GetUser(email, false);

            if (membershipUser == null)
            {
                try
                {
                    using (DataContext _db = new DataContext())
                    {
                        User user = new User();
                        user.Email = email;
                        
                        user.Password = Crypto.HashPassword(password);
                      
                        if (_db.Roles.Find(2) != null)
                        {
                            user.RoleID = 2;
                        }

                        _db.Users.Add(user);
                        _db.SaveChanges();
                        membershipUser = GetUser(email, false);
                        return membershipUser;
                    }
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }
        public override bool ValidateUser(string username, string password)
        {
            bool isValid = false;

            using (DataContext _db = new DataContext())
            {
                try
                {
                    User user = (from u in _db.Users
                                 where u.Email == username
                                 select u).FirstOrDefault();


                    if (user != null && Crypto.VerifyHashedPassword(user.Password,password))
                    {
                        isValid = true;
                    }
                }
                catch
                {
                    isValid = false;
                }
            }
            return isValid;
        }
 public override string[] GetRolesForUser(string username)
 {
     string[] role = new string[] { };
     using (DataContext db = new DataContext())
     {
         // Получаем пользователя
         User user = db.Users.FirstOrDefault(u => u.Email == username);
         if (user != null)
         {
             // получаем роль
             Role userRole = db.Roles.Find(user.RoleID);
             if (userRole != null)
                 role = new string[] { userRole.Name };
         }
     }
     return role;
 }
 public override bool IsUserInRole(string username, string roleName)
 {
     bool outputResult = false;
     // Находим пользователя
     using (DataContext db = new DataContext())
     {
         // Получаем пользователя
         User user = db.Users.FirstOrDefault(u => u.Email == username);
         if (user != null)
         {
             // получаем роль
             Role userRole = db.Roles.Find(user.RoleID);
             //сравниваем
             if (userRole != null && userRole.Name == roleName)
                 outputResult = true;
         }
     }
     return outputResult;
 }
 public override MembershipUser GetUser(string email, bool userIsOnline)
 {
     try
     {
         using (DataContext _db = new DataContext())
         {
             var users = from u in _db.Users
                         where u.Email == email
                         select u;
             if (users.Count() > 0)
             {
                 User user = users.First();
                 MembershipUser memberUser = new MembershipUser("MyMembershipProvider", user.Email, null, null, null, null,
                     false, false, user.CreationDate, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
                 return memberUser;
             }
         }
     }
     catch
     {
         return null;
     }
     return null;
 }
        public override string[] GetAllRoles()
        {
            string[] roles = new string[] { };
            using (DataContext db = new DataContext())
            {
                var allRoles = db.Roles.ToList();

                for (int i = 0; i < allRoles.Count(); i++)
                {
                    roles[i] = allRoles[i].Name.ToString();
                }
            }

            return roles;
        }