Example #1
0
        private void AddRolesToUser(
            IEnumerable <string> roles,
            ApplicationUser user,
            UserStore <ApplicationUser> store,
            string connectionString)
        {
            IUserRoleStore <ApplicationUser, string> userRoleStore = store;

            using (var roleStore = new RoleStore <IdentityRole>(new ApplicationDbContext <ApplicationUser>(connectionString)))
            {
                IList <string> userRoles = userRoleStore.GetRolesAsync(user)
                                           .GetAwaiter().GetResult();

                foreach (string roleName in roles)
                {
                    if (roleStore.FindByNameAsync(roleName).GetAwaiter().GetResult() == null)
                    {
                        roleStore.CreateAsync(new IdentityRole {
                            Name = roleName
                        })
                        .GetAwaiter()
                        .GetResult();
                    }

                    if (!userRoles.Contains(roleName))
                    {
                        userRoleStore.AddToRoleAsync(user, roleName)
                        .GetAwaiter()
                        .GetResult();
                    }
                }
            }
        }
 public TokenStoreService(ApplicationDbContext applicationDbContext, IOptionsSnapshot <BearerTokensOptions> configuration, ISecurityService securityService, IUserRoleStore <User> userRoleStore)
 {
     _applicationDbContext = applicationDbContext;
     _configuration        = configuration;
     _securityService      = securityService;
     _userRoleRoleStore    = userRoleStore;
 }
        async Task <bool> IUserRoleStore <JoinIdentityUser> .IsInRoleAsync(JoinIdentityUser user, string roleName, CancellationToken cancellationToken)
        {
            IUserRoleStore <JoinIdentityUser> self = this;
            var roles = await self.GetRolesAsync(user, cancellationToken);

            return(roles.Contains(roleName));
        }
Example #4
0
        public override async Task <IdentityResult> AddToRoleAsync(Guid userId, string role)
        {
            IUserRoleStore <User, Guid> userRoleStore = GetUserRoleStore();
            User tUser = await FindByIdAsync(userId).WithCurrentCulture <User>();

            if (tUser == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "User Id Not Found", new object[]
                {
                    userId
                }));
            }
            IList <string> list = await userRoleStore.GetRolesAsync(tUser).WithCurrentCulture <IList <string> >();

            IdentityResult result;

            if (list.Contains(role))
            {
                result = new IdentityResult(new string[]
                {
                    "User Already in Role"
                });
            }
            else
            {
                await userRoleStore.AddToRoleAsync(tUser, role).WithCurrentCulture();

                result = await this.UpdateAsync(tUser).WithCurrentCulture <IdentityResult>();
            }
            return(result);
        }
Example #5
0
 public ExtendUserManager(IUserStore <TUser> store,
                          RoleManager <Roles> roleManager,
                          IOrganizationExpansionStore organizationExpansionStore,
                          IMapper mapper,
                          IUserRoleStore userRoleStore,
                          ExtendUserStore <ApplicationDbContext> extendUserStore,
                          IOrganizationStore organizationStore,
                          IOptions <IdentityOptions> optionsAccessor,
                          IPasswordHasher <TUser> passwordHasher,
                          IEnumerable <IUserValidator <TUser> > userValidators,
                          IEnumerable <IPasswordValidator <TUser> > passwordValidators,
                          ILookupNormalizer keyNormalizer,
                          IdentityErrorDescriber errors,
                          IServiceProvider services,
                          ILogger <UserManager <TUser> > logger)
     : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
 {
     Store                       = store ?? throw new ArgumentNullException(nameof(store));
     _extendUserStore            = extendUserStore ?? throw new ArgumentNullException(nameof(extendUserStore));
     _organizationStore          = organizationStore ?? throw new ArgumentNullException(nameof(organizationStore));
     _mapper                     = mapper ?? throw new ArgumentNullException(nameof(mapper));
     _userRoleStore              = userRoleStore ?? throw new ArgumentNullException(nameof(userRoleStore));
     _roleManager                = roleManager ?? throw new ArgumentNullException(nameof(roleManager));
     _organizationExpansionStore = organizationExpansionStore ?? throw new ArgumentNullException(nameof(organizationExpansionStore));
 }
 public RoleApplicationManager(IRoleApplicationStore roleApplicationStore,
                               IUserRoleStore userRoleStore,
                               ExtendUserManager <Users> extendUserManager)
 {
     Store = roleApplicationStore ?? throw new ArgumentNullException(nameof(roleApplicationStore));
     _extendUserManager = extendUserManager ?? throw new ArgumentNullException(nameof(extendUserManager));
     _userRoleStore     = userRoleStore ?? throw new ArgumentNullException(nameof(userRoleStore));
 }
Example #7
0
        private async Task <IList <string> > GetUserToRoles(UserStore <ApplicationUser> store, ApplicationUser user)
        {
            IUserRoleStore <ApplicationUser, string> userRoleStore = store;

            using (new RoleStore <IdentityRole>(new ApplicationDbContext <ApplicationUser>("EPiServerDB")))
            {
                return(await userRoleStore.GetRolesAsync(user));
            }
        }
Example #8
0
 public UserDisplayDriver(
     UserManager <IUser> userManager,
     IRoleService roleService,
     IUserRoleStore <IUser> userRoleStore,
     IStringLocalizer <UserDisplayDriver> stringLocalizer)
 {
     _userManager   = userManager;
     _roleService   = roleService;
     _userRoleStore = userRoleStore;
     T = stringLocalizer;
 }
 public UnitTestUserRoleStore(
     IUserTestFactory <TUser> userTestFactory,
     IRoleTestFactory <TRole> roleTestFactory,
     IUserRoleTestFactory <TUser> userRoleTestFactory)
 {
     _userTestFactory     = userTestFactory;
     _userStore           = _userTestFactory.UserStore;
     _roleTestFactory     = roleTestFactory;
     _roleStore           = roleTestFactory.RoleStore;
     _userRoleTestFactory = userRoleTestFactory;
     _userRoleStore       = userRoleTestFactory.UserRoleStore;
 }
Example #10
0
 public ApplicationUserManager(IUserRoleStore <UserIdentity, int> store, ILdapAuthClient ldapAuth) : base(store)
 {
     if (store == null)
     {
         throw new ArgumentNullException("store");
     }
     if (ldapAuth == null)
     {
         throw new ArgumentNullException("ldapAuth");
     }
     _ldapAuth = ldapAuth;
     _store    = store;
 }
Example #11
0
        public UserManager(IUserStore userStore, ILogger <UserManager> logger, IMapper mapper, ITransaction <AuthenticationDbContext> transaction, IRoleStore roleStore, IUserRoleStore userRoleStore, IRolePermissionStore rolePermissionStore, IPermissionStore permissionStore)


        {
            _rolePermissionStore = rolePermissionStore;
            _permissionStore     = permissionStore;
            _transaction         = transaction;
            _roleStore           = roleStore;
            _userRoleStore       = userRoleStore;
            _userStore           = userStore;
            _logger = logger;
            _mapper = mapper;
        }
 public UserDisplayDriver(
     UserManager <IUser> userManager,
     IRoleService roleService,
     IUserRoleStore <IUser> userRoleStore,
     ILogger <UserDisplayDriver> logger,
     IEnumerable <IUserEventHandler> handlers,
     IStringLocalizer <UserDisplayDriver> stringLocalizer)
 {
     _userManager   = userManager;
     _roleService   = roleService;
     _userRoleStore = userRoleStore;
     _logger        = logger;
     Handlers       = handlers;
     S = stringLocalizer;
 }
Example #13
0
 public AccountController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     IEmailSender emailSender,
     ILogger <AccountController> logger,
     IOptions <AccountOptions> options,
     IUserRoleStore <ApplicationUser> userRoleStore)
 {
     _userManager    = userManager;
     _signInManager  = signInManager;
     _emailSender    = emailSender;
     _logger         = logger;
     _accountOptions = options;
     _userRoleStore  = userRoleStore;
 }
Example #14
0
        public DiTestController(
            // the Microsoft.AspNetCore.Identity User and Role Manager classes
            RoleManager <IdentityRole> roleManager,
            UserManager <ApplicationUser> userManager,

            IIdentityDatabaseContext <ApplicationUser, IdentityRole, string> identityDatabaseContext,

            // if want to use with SOLID and Interface Segregation Principle, then can just use the specific interface that need

            // these interfaces are all implemented by UserStore
            IUserStore <ApplicationUser> userStore,
            IUserLoginStore <ApplicationUser> userLoginStore,
            IUserRoleStore <ApplicationUser> userRoleStore,
            IUserClaimStore <ApplicationUser> userClaimStore,
            IUserPasswordStore <ApplicationUser> userPasswordStore,
            IUserSecurityStampStore <ApplicationUser> userSecurityStampStore,
            IUserEmailStore <ApplicationUser> userEmailStore,
            IUserLockoutStore <ApplicationUser> userLockoutStore,
            IUserPhoneNumberStore <ApplicationUser> userPhoneNumberStore,
            IUserTwoFactorStore <ApplicationUser> userTwoFactorStore,
            IQueryableUserStore <ApplicationUser> queryableUserStore,

            // these interfaces are all implemented by RoleStore
            IRoleStore <IdentityRole> roleStore,
            IRoleClaimStore <IdentityRole> roleClaimStore,
            IQueryableRoleStore <IdentityRole> queryableRoleStore
            )
        {
            _roleManager = roleManager;
            _userManager = userManager;

            _identityDatabaseContext = identityDatabaseContext;
            _userStore              = userStore;
            _userLoginStore         = userLoginStore;
            _userRoleStore          = userRoleStore;
            _userClaimStore         = userClaimStore;
            _userPasswordStore      = userPasswordStore;
            _userSecurityStampStore = userSecurityStampStore;
            _userEmailStore         = userEmailStore;
            _userLockoutStore       = userLockoutStore;
            _userPhoneNumberStore   = userPhoneNumberStore;
            _userTwoFactorStore     = userTwoFactorStore;
            _queryableUserStore     = queryableUserStore;

            _roleStore          = roleStore;
            _roleClaimStore     = roleClaimStore;
            _queryableRoleStore = queryableRoleStore;
        }
Example #15
0
 public UserRoleDisplayDriver(
     UserManager <IUser> userManager,
     IRoleService roleService,
     IUserRoleStore <IUser> userRoleStore,
     IHttpContextAccessor httpContextAccessor,
     INotifier notifier,
     IAuthorizationService authorizationService,
     IHtmlLocalizer <UserRoleDisplayDriver> htmlLocalizer)
 {
     _userManager          = userManager;
     _roleService          = roleService;
     _userRoleStore        = userRoleStore;
     _httpContextAccessor  = httpContextAccessor;
     _notifier             = notifier;
     _authorizationService = authorizationService;
     H = htmlLocalizer;
 }
Example #16
0
        public override async void AddChanges()
        {
            IDatabaseConnectionResolver databaseConnectionResolver = ServiceLocator.Current.GetInstance <IDatabaseConnectionResolver>();

            using (UserStore <ApplicationUser> store = new UserStore <ApplicationUser>(new IdentityDbContext <ApplicationUser>(databaseConnectionResolver.Resolve().ConnectionString)))
            {
                //If there's already a user, then we don't need a seed
                if (store.Users.Any())
                {
                    return;
                }

                //We know that this Password hasher is used as it's configured
                IPasswordHasher hasher       = new PasswordHasher();
                string          passwordHash = hasher.HashPassword("3p!Pass");

                ApplicationUser administrator = new ApplicationUser();
                administrator.Email          = "*****@*****.**";
                administrator.EmailConfirmed = true;
                administrator.LockoutEnabled = true;
                administrator.UserName       = "******";
                administrator.PasswordHash   = passwordHash;
                administrator.SecurityStamp  = Guid.NewGuid().ToString();

                await store.CreateAsync(administrator);

                //Get the user associated with our username
                ApplicationUser createdUser = await store.FindByNameAsync("initialadministrator");

                IUserRoleStore <ApplicationUser, string> userRoleStore = store as IUserRoleStore <ApplicationUser, string>;

                IList <string> userRoles = await userRoleStore.GetRolesAsync(createdUser);

                foreach (string roleName in this._roles)
                {
                    if (!userRoles.Contains(roleName))
                    {
                        await userRoleStore.AddToRoleAsync(createdUser, roleName);
                    }
                }

                await store.UpdateAsync(createdUser);
            }
        }
Example #17
0
 public UserInformationDisplayDriver(
     UserManager <IUser> userManager,
     IRoleService roleService,
     IUserRoleStore <IUser> userRoleStore,
     IHttpContextAccessor httpContextAccessor,
     INotifier notifier,
     IAuthorizationService authorizationService,
     ILogger <UserDisplayDriver> logger,
     IHtmlLocalizer <UserDisplayDriver> htmlLocalizer,
     IStringLocalizer <UserDisplayDriver> stringLocalizer)
 {
     _userManager          = userManager;
     _roleService          = roleService;
     _userRoleStore        = userRoleStore;
     _httpContextAccessor  = httpContextAccessor;
     _notifier             = notifier;
     _authorizationService = authorizationService;
     _logger = logger;
     H       = htmlLocalizer;
     S       = stringLocalizer;
 }
Example #18
0
 public UserDisplayDriver(
     UserManager <IUser> userManager,
     IRoleService roleService,
     IUserRoleStore <IUser> userRoleStore,
     IHttpContextAccessor httpContextAccessor,
     INotifier notifier,
     ILogger <UserDisplayDriver> logger,
     IEnumerable <IUserEventHandler> handlers,
     IAuthorizationService authorizationService,
     IHtmlLocalizer <UserDisplayDriver> htmlLocalizer)
 {
     _userManager          = userManager;
     _roleService          = roleService;
     _userRoleStore        = userRoleStore;
     _httpContextAccessor  = httpContextAccessor;
     _notifier             = notifier;
     _authorizationService = authorizationService;
     _logger  = logger;
     Handlers = handlers;
     H        = htmlLocalizer;
 }
Example #19
0
        private void AddUserToRoles(UserStore <ApplicationUser> store, ApplicationUser user, string[] roles)
        {
            IUserRoleStore <ApplicationUser, string> userRoleStore = store;

            using (var roleStore = new RoleStore <IdentityRole>(new ApplicationDbContext <ApplicationUser>("EPiServerDB")))
            {
                IList <string> userRoles = userRoleStore.GetRolesAsync(user).GetAwaiter().GetResult();
                foreach (string roleName in roles)
                {
                    if (roleStore.FindByNameAsync(roleName).GetAwaiter().GetResult() == null)
                    {
                        roleStore.CreateAsync(new IdentityRole {
                            Name = roleName
                        }).GetAwaiter().GetResult();
                    }
                    if (!userRoles.Contains(roleName))
                    {
                        userRoleStore.AddToRoleAsync(user, roleName).GetAwaiter().GetResult();
                    }
                }
            }
        }
Example #20
0
        private IUserRoleStore <User, Guid> GetUserRoleStore()
        {
            IUserRoleStore <User, Guid> userRoleStore = this.Store as IUserRoleStore <User, Guid>;

            return(userRoleStore);
        }
Example #21
0
 public UserService(IUserDataStore userDataStore, IUserRoleStore userRoleStore)
 {
     this.userDataStore = userDataStore;
     this.userRoleStore = userRoleStore;
 }
Example #22
0
 public AdminController(IUserSearch<UserIdentity> userSearch, IUserStore<UserIdentity> userStore, IUserRoleStore<UserIdentity> roleStore)
 {
     _userSearch = userSearch;
     _userStore = userStore;
     _roleStore = roleStore;
 }
Example #23
0
 public ApplicationUserManager(IUserRoleStore <TUser, TKey> store) : base(store)
 {
 }
Example #24
0
 public RoleManger(IRoleStore store, IPermissionStore permissionStore, IOrganizationStore organizationStore, IUserRoleStore userRoleStore, IUserOrgStore userOrgStore, IRoleOrgStore roleOrgStore, IRoleOrgPerStore roleOrgPerStore, IMapper mapper)
 {
     Store             = store ?? throw new ArgumentNullException(nameof(store));
     PermissionStore   = permissionStore ?? throw new ArgumentNullException(nameof(permissionStore));
     OrganizationStore = organizationStore ?? throw new ArgumentNullException(nameof(organizationStore));
     UserRoleStore     = userRoleStore ?? throw new ArgumentNullException(nameof(userRoleStore));
     UserOrgStore      = userOrgStore ?? throw new ArgumentNullException(nameof(userOrgStore));
     RoleOrgStore      = roleOrgStore ?? throw new ArgumentNullException(nameof(roleOrgStore));
     RoleOrgPerStore   = roleOrgPerStore ?? throw new ArgumentNullException(nameof(roleOrgPerStore));
     Mapper            = mapper ?? throw new ArgumentNullException(nameof(mapper));
 }
 /// <summary>
 /// 构造器
 /// </summary>
 /// <param name="store"></param>
 public UserRoleManager(IUserRoleStore store)
 {
     Store = store;
 }
Example #26
0
 public ApplicationUserManager(IUserRoleStore <User, string> userStore)
     : base(userStore)
 {
 }
Example #27
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="userRoleStore"></param>
 /// <param name="roleOrgPerStore"></param>
 /// <param name="organizationStore"></param>
 public RoleOrgPerManager(IUserRoleStore userRoleStore, IRoleOrgPerStore roleOrgPerStore, IOrganizationStore organizationStore)
 {
     UserRoleStore     = userRoleStore ?? throw new ArgumentNullException(nameof(userRoleStore));
     RoleOrgPerStore   = roleOrgPerStore ?? throw new ArgumentNullException(nameof(roleOrgPerStore));
     OrganizationStore = organizationStore ?? throw new ArgumentNullException(nameof(organizationStore));
 }
Example #28
0
 public StorageIdentityPrincipalFactory(IUserRoleStore <TUser> UserRoleStore)
 {
     this.UserRoleStore = UserRoleStore;
 }
Example #29
0
 public UserRoleService(IUserRoleStore userRoleStore)
 {
     this.userRoleStore = userRoleStore;
 }