示例#1
0
 public IamMultiTenantUserClaimsPrincipalFactory(UserManager <TUser> userManager, RoleManager <TRole> roleManager, IMultiTenantUserClaimStore <TUser, TTenantKey> userClaimStore, IMultiTenantUserRoleStore <TUser, TTenantKey> roleStore, IMultiTenantRoleClaimStore <TRole, TTenantKey> roleClaimStore, IOptions <IdentityOptions> identityOptions) : base(userManager, identityOptions)
 {
     _userManager    = userManager;
     _roleManager    = roleManager;
     _userClaimStore = userClaimStore;
     _roleStore      = roleStore;
     _roleClaimStore = roleClaimStore;
 }
        public void Init()
        {
            var connectionString = ConfigurationHelper.GetConnectionString(true);

            var services = new ServiceCollection();

            services.AddTransient(typeof(IMultiTenantRoleClaimStore <Role, long>), typeof(MultiTenantRoleClaimStore <User, Role, long, long>));

            var builder = services.AddIdentity <User, Role>()
                          .AddEntityFrameworkStores <MultiTenantIamDbContext <User, Role, long, long> >()
                          .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Bearer";
                options.DefaultChallengeScheme    = "Bearer";
            });

            services.AddAuthorization();

            services.AddMultiTenantIamCore <long>();

            services.AddDbContext <MultiTenantIamDbContext <User, Role, long, long> >(options =>
                                                                                      options.UseSqlServer(connectionString));

            serviceProvider = services.BuildServiceProvider();

            using (var scope = serviceProvider.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService(typeof(MultiTenantIamDbContext <User, Role, long, long>)) as MultiTenantIamDbContext <User, Role, long, long>;

                dbContext.Database.EnsureCreated();

                new Checkpoint().Reset(connectionString).Wait();
            }

            roleManager = serviceProvider.GetRequiredService(typeof(RoleManager <Role>)) as RoleManager <Role>;

            claimStore = serviceProvider.GetRequiredService(typeof(IMultiTenantRoleClaimStore <Role, long>)) as IMultiTenantRoleClaimStore <Role, long>;

            roleManager.CreateAsync(new Role()
            {
                Name = "test",
            }).Wait();

            role = roleManager.FindByNameAsync("test").Result;
        }
示例#3
0
        public void Init()
        {
            var services = new ServiceCollection();

            services.AddTransient(typeof(IMultiTenantRoleClaimStore <Role, long>), typeof(MemoryMultiTenantStore <User, Role, long, long>));

            var builder = services.AddIdentity <User, Role>()
                          .AddEntityFrameworkStores <IdentityDbContext <User, Role, long> >()
                          .AddDefaultTokenProviders();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = "Bearer";
                options.DefaultChallengeScheme    = "Bearer";
            });

            services.AddAuthorization();

            services.AddIamCore();

            services.AddDbContext <IdentityDbContext <User, Role, long> >(options =>
                                                                          options.UseInMemoryDatabase("test"));

            serviceProvider = services.BuildServiceProvider();

            roleManager = serviceProvider.GetRequiredService(typeof(RoleManager <Role>)) as RoleManager <Role>;

            claimStore = serviceProvider.GetRequiredService(typeof(IMultiTenantRoleClaimStore <Role, long>)) as IMultiTenantRoleClaimStore <Role, long>;

            roleManager.CreateAsync(new Role()
            {
                Name = "test",
            }).Wait();

            role = roleManager.FindByNameAsync("test").Result;
        }
示例#4
0
        private static void AddResourceIdAccessMt(string roleName, string[] policies, long[] tenantIds, RoleManager <Role> roleManager, IMultiTenantRoleClaimStore <Role, long> claimStore, bool hasAccessToAll, params long[] resourceIds)
        {
            var role = roleManager.FindByNameAsync(roleName).Result;

            foreach (var policy in policies)
            {
                foreach (var tenantId in tenantIds)
                {
                    if (hasAccessToAll)
                    {
                        roleManager.GrantAccessToAllResources <Role, long>(claimStore, role, tenantId, policy).Wait();
                    }
                    else
                    {
                        roleManager.GrantAccessToResources <Role, long, long>(claimStore, role, tenantId, policy, resourceIds).Wait();
                    }
                }
            }
        }