示例#1
0
        public override bool IsUserInRole(string username, string roleName)
        {
            bool outputResult = false;
            // Находим пользователя
            using (AlbumDbEntities _db = new AlbumDbEntities())
            {
                try
                {
                    // Получаем пользователя
                    Users agent = (from u in _db.Users
                                   where u.Email == username
                                   select u).FirstOrDefault();
                    if (agent != null)
                    {
                        // получаем роль
                        ORM.Roles agentRole = agent.Roles.FirstOrDefault(u => u.Name == roleName);

                        //сравниваем
                        if (agentRole != null && agentRole.Name == roleName)
                        {
                            outputResult = true;
                        }
                    }
                }
                catch
                {
                    outputResult = false;
                }
            }
            return outputResult;
        }
示例#2
0
 public override void CreateRole(string roleName)
 {
     var newRole = new ORM.Roles() { Name = roleName };
     using (var context = new AlbumDbEntities())
     {
         context.Roles.Add(newRole);
         context.SaveChanges();
     }
 }
示例#3
0
        public override string[] GetRolesForUser(string email)
        {
            using (var context = new AlbumDbEntities())
            {
                var roles = new string[]{ };
                var user = context.Users.FirstOrDefault(u => u.Email == email);

                if (user == null) return roles;

                var userRoles = user.Roles.ToList();

                int i = 0;
                roles = new string[userRoles.Count];
                foreach (var role in userRoles)
                {
                    roles[i] = role.Name;
                    i++;
                }
                return roles;
            }
        }
        //private IUserRepository userRepository;//IAccountService!!!
        public MembershipUser CreateUser(string email, string password)
        {
            //userRepository = (IUserRepository)DependencyResolver.Current.GetService(typeof(IUserRepository));

            MembershipUser membershipUser = GetUser(email, false);

            if (membershipUser != null)
            {
                return null;
            }

            using (var context = new AlbumDbEntities())
            {
                var user = new Users
                {
                    Email = email,
                    Password = Crypto.HashPassword(password),
                    //http://msdn.microsoft.com/ru-ru/library/system.web.helpers.crypto(v=vs.111).aspx
                };

                var role = context.Roles.FirstOrDefault(r => r.Name == "user");
                if (role != null)
                {
                    user.Roles.Add(role);
                }
                if (!context.Users.Any())
                {
                    var adminRole = context.Roles.FirstOrDefault(r => r.Name == "admin");
                    if (adminRole != null)
                    {
                        user.Roles.Add(adminRole);
                    }
                }

                user.Name = "name";

                context.Users.Add(user);
                context.SaveChanges();
                membershipUser = GetUser(user.Email, false);
                return membershipUser;
            }
        }
        public override bool ValidateUser(string email, string password)
        {
            using (var context = new AlbumDbEntities())
            {
                Users user = (from u in context.Users
                             where u.Email == email
                             select u).FirstOrDefault();

                if (user != null && Crypto.VerifyHashedPassword(user.Password, password))
                //Определяет, соответствуют ли заданный хэш RFC 2898 и пароль друг другу
                {
                    return true;
                }
            }
            return false;
        }
        public override MembershipUser GetUser(string email, bool userIsOnline)
        {
            using (var context = new AlbumDbEntities())
            {
                var user = (from u in context.Users
                            where u.Email == email
                            select u).FirstOrDefault();

                if (user == null) return null;

                var memberUser = new MembershipUser("CustomMembershipProvider", user.Name,
                    null, null, null, null,
                    false, false, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);

                return memberUser;
            }
        }