Exemple #1
0
        public static void ConfigureTenant(this ModelBuilder builder, out TenancyModelState <string> tenancyModelState)
        {
            // MultiTenancyServer configuration.
            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <Tenant, string>(tenantStoreOptions);

            // Add multi-tenancy support to model.
            var tenantReferenceOptions = new TenantReferenceOptions();

            builder.HasTenancy(tenantReferenceOptions, out tenancyModelState);
        }
Exemple #2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // MultiTenancyServer configuration.
            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <ApplicationTenant, string>(tenantStoreOptions);

            // Configure custom properties on ApplicationTenant.
            builder.Entity <ApplicationTenant>(b =>
            {
                b.Property(t => t.DisplayName).HasMaxLength(256);
            });
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            // MultiTenancyServer configuration.
            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <ApplicationTenant, string>(tenantStoreOptions);

            // Add multi-tenancy support to model.
            var tenantReferenceOptions = new TenantReferenceOptions();

            builder.HasTenancy <string>(tenantReferenceOptions, out _tenancyModelState);

            // Configure custom properties on ApplicationTenant.
            builder.Entity <ApplicationTenant>(b =>
            {
                b.Property(t => t.DisplayName).HasMaxLength(256);
            });

            // Configure properties on User (ASP.NET Core Identity).
            builder.Entity <ApplicationUser>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedUserName.
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedUserName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApplicationUser.NormalizedUserName))
                .HasName("TenantUserNameIndex").IsUnique();
            });

            // Configure properties on Role (ASP.NET Core Identity).
            builder.Entity <IdentityRole>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedUserName.
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedUserName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(IdentityRole.NormalizedName))
                .HasName("TenantRoleNameIndex").IsUnique();
            });
        }
Exemple #4
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            var configurationStoreOptions = new ConfigurationStoreOptions();

            builder.ConfigureClientContext(configurationStoreOptions);
            builder.ConfigureResourcesContext(configurationStoreOptions);

            var operationalStoreOptions = new OperationalStoreOptions();

            builder.ConfigurePersistedGrantContext(operationalStoreOptions);

            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <ApplicationTenant, ObjectId>(tenantStoreOptions);

            var tenantReferenceOptions = new TenantReferenceOptions();

            builder.HasTenancy <string>(tenantReferenceOptions, out _tenancyModelState);

            builder.Entity <ApplicationTenant>(b =>
            {
                b.Property(t => t.DisplayName).HasMaxLength(256);
            });

            // Configure properties on User (ASP.NET Core Identity).
            builder.Entity <ApplicationUser>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedUserName.
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedUserName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApplicationUser.NormalizedUserName))
                .HasName("TenantUserNameIndex").IsUnique();
            });

            // Configure properties on Role (ASP.NET Core Identity).
            builder.Entity <ApplicationRole>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedName.
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApplicationRole.NormalizedName))
                .HasName("TenantRoleNameIndex").IsUnique();
            });

            builder.Entity <Client>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(c => c.ClientId).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(Client.ClientId)).IsUnique();
            });

            builder.Entity <IdentityResource>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(r => r.Id).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(IdentityResource.Id)).IsUnique();
            });

            builder.Entity <ApiResource>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(r => r.Id).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApiResource.Id)).IsUnique();
            });

            builder.Entity <ApiScope>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(s => s).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApiScope)).IsUnique();
            });

            builder.Entity <PersistedGrant>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, indexNameFormat: $"IX_{nameof(PersistedGrant)}_{{0}}");
            });

            builder.Entity <IdentityServer4.EntityFramework.Entities.DeviceFlowCodes>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(c => c.DeviceCode).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(IdentityServer4.EntityFramework.Entities.DeviceFlowCodes.DeviceCode)).IsUnique();
            });
        }