コード例 #1
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            var storeOptions                = _identityOptions.Stores;
            var maxKeyLength                = storeOptions?.MaxLengthForKeys ?? 0;
            var encryptPersonalData         = storeOptions?.ProtectPersonalData ?? false;
            PersonalDataConverter converter = null;

            if (storeOptions.ProtectPersonalData)
            {
                converter = new PersonalDataConverter(this.GetService <IPersonalDataProtector>());
            }

            builder.ApplyConfiguration(new UserConfiguration(storeOptions, converter));
            builder.ApplyConfiguration(new UserClaimConfiguration());
            builder.ApplyConfiguration(new UserFingerprintConfiguration());
            builder.ApplyConfiguration(new UserKeyConfiguration());
            builder.ApplyConfiguration(new UserLoginConfiguration(storeOptions));
            builder.ApplyConfiguration(new UserTokenConfiguration(storeOptions, converter));
            builder.ApplyConfiguration(new RoleConfiguration());
            builder.ApplyConfiguration(new RoleClaimConfiguration());
            builder.ApplyConfiguration(new UserRoleConfiguration());
        }
コード例 #2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            const int  maxKeyLength        = 128;
            const bool encryptPersonalData = true;
            var        converter           = new PersonalDataConverter(new PersonalDataProtector());

            builder.Entity <TUser>(
                b =>
            {
                b.HasKey(u => u.Id);
                b.HasIndex(u => u.NormalizedUserName).IsUnique();
                b.HasIndex(u => u.NormalizedEmail);
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);

                if (encryptPersonalData)
                {
                    var personalDataProps = typeof(TUser).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in personalDataProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException("Resources.CanOnlyProtectStrings");
                        }

                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                b.HasMany <TUserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
                b.HasMany <TUserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
                b.HasMany <TUserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
            });

            builder.Entity <TUserClaim>(
                b =>
            {
                b.HasKey(uc => uc.Id);
            });

            builder.Entity <TUserLogin>(
                b =>
            {
                b.HasKey(
                    l => new { l.LoginProvider, l.ProviderKey });

                if (maxKeyLength > 0)
                {
                    b.Property(l => l.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(l => l.ProviderKey).HasMaxLength(maxKeyLength);
                }
            });

            builder.Entity <TUserToken>(
                b =>
            {
                b.HasKey(
                    t => new
                {
                    t.UserId,
                    t.LoginProvider,
                    t.Name
                });

                if (maxKeyLength > 0)
                {
                    b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(t => t.Name).HasMaxLength(maxKeyLength);
                }

                if (encryptPersonalData)
                {
                    var tokenProps = typeof(TUserToken).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in tokenProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException("Resources.CanOnlyProtectStrings");
                        }

                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }
            });
        }
コード例 #3
0
        private void IdentityUserContextBase(ModelBuilder builder)
        {
            StoreOptions storeOptions = this.GetService <IDbContextOptions>()
                                        .Extensions.OfType <CoreOptionsExtension>()
                                        .FirstOrDefault()?.ApplicationServiceProvider
                                        ?.GetService <IOptions <IdentityOptions> >()
                                        ?.Value?.Stores;

            var maxKeyLength                = storeOptions?.MaxLengthForKeys ?? 0;
            var encryptPersonalData         = storeOptions?.ProtectPersonalData ?? false;
            PersonalDataConverter converter = null;

            builder.Entity <MultiTenantUserIdentity>(b =>
            {
                b.HasKey(u => u.Id);
                //b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex");
                b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
                //b.ToTable("AspNetUsers");
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);

                if (encryptPersonalData)
                {
                    converter             = new PersonalDataConverter(this.GetService <IPersonalDataProtector>());
                    var personalDataProps = typeof(MultiTenantUserIdentity).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in personalDataProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException("Can only protect strings");
                        }
                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                b.HasMany <UserIdentityUserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
                b.HasMany <UserIdentityUserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
                b.HasMany <UserIdentityUserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
            });

            builder.Entity <UserIdentityUserClaim>(b =>
            {
                b.HasKey(uc => uc.Id);
                //b.ToTable("AspNetUserClaims");
            });

            builder.Entity <UserIdentityUserLogin>(b =>
            {
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });

                if (maxKeyLength > 0)
                {
                    b.Property(l => l.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(l => l.ProviderKey).HasMaxLength(maxKeyLength);
                }

                //b.ToTable("AspNetUserLogins");
            });

            builder.Entity <UserIdentityUserToken>(b =>
            {
                b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });

                if (maxKeyLength > 0)
                {
                    b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(t => t.Name).HasMaxLength(maxKeyLength);
                }

                if (encryptPersonalData)
                {
                    var tokenProps = typeof(UserIdentityUserToken).GetProperties()
                                     .Where(prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in tokenProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException("Can only protect strings");
                        }
                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                //b.ToTable("AspNetUserTokens");
            });
        }
コード例 #4
0
        protected void IdentityUserContext_OnModelCreating <TUser, TKey, TUserToken, TUserClaim, TUserLogin>(ModelBuilder builder)
            where TUser : ApplicationUser
            where TKey : IEquatable <TKey>
            where TUserClaim : IdentityUserClaim <TKey>
            where TUserLogin : IdentityUserLogin <TKey>
            where TUserToken : IdentityUserToken <TKey>
        {
            var storeOptions                = GetStoreOptions();
            var maxKeyLength                = storeOptions?.MaxLengthForKeys ?? 0;
            var encryptPersonalData         = storeOptions?.ProtectPersonalData ?? false;
            PersonalDataConverter converter = null;

            builder.Entity <TUser>(b =>
            {
                b.HasKey(u => u.Id);
                b.HasIndex(u => new { u.NormalizedUserName, u.TenantId }).HasName("UserNameTenantIndex").IsUnique();
                b.HasIndex(u => new { u.NormalizedEmail, u.TenantId }).HasName("EmailTenantIndex");
                b.ToTable("AspNetUsers");
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);

                if (encryptPersonalData)
                {
                    converter             = new PersonalDataConverter(this.GetService <IPersonalDataProtector>());
                    var personalDataProps = typeof(TUser).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in personalDataProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException("Protection is supported only on string fields");
                        }
                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                b.HasMany <TUserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
                b.HasMany <TUserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
                b.HasMany <TUserToken>().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
            });

            builder.Entity <TUserClaim>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("AspNetUserClaims");
            });

            builder.Entity <TUserLogin>(b =>
            {
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });

                if (maxKeyLength > 0)
                {
                    b.Property(l => l.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(l => l.ProviderKey).HasMaxLength(maxKeyLength);
                }

                b.ToTable("AspNetUserLogins");
            });

            builder.Entity <TUserToken>(b =>
            {
                b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });

                if (maxKeyLength > 0)
                {
                    b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(t => t.Name).HasMaxLength(maxKeyLength);
                }

                if (encryptPersonalData)
                {
                    var tokenProps = typeof(TUserToken).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in tokenProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException("Protection is supported only on string fields");
                        }
                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                b.ToTable("AspNetUserTokens");
            });
        }
コード例 #5
0
        /// <summary>
        /// Configures the schema needed for the identity framework.
        /// </summary>
        /// <param name="builder">
        /// The builder being used to construct the model for this context.
        /// </param>
        protected override void OnModelCreating(ModelBuilder builder)
        {
            var storeOptions                = GetStoreOptions();
            var maxKeyLength                = storeOptions?.MaxLengthForKeys ?? 0;
            var encryptPersonalData         = storeOptions?.ProtectPersonalData ?? false;
            PersonalDataConverter converter = null;

            builder.Entity <ApplicationUser>(b =>
            {
                b.HasKey(u => u.Id);
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique();
                b.HasIndex(u => u.NormalizedEmail).HasName("EmailIndex");
                b.ToTable("EonUser");
                b.Property(u => u.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.UserName).HasMaxLength(256);
                b.Property(u => u.NormalizedUserName).HasMaxLength(256);
                b.Property(u => u.Email).HasMaxLength(256);
                b.Property(u => u.NormalizedEmail).HasMaxLength(256);

                if (encryptPersonalData)
                {
                    converter             = new PersonalDataConverter(this.GetService <IPersonalDataProtector>());
                    var personalDataProps = typeof(ApplicationUser).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in personalDataProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException();
                        }
                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                b.HasMany <ApplicationUserClaim>().WithOne().HasForeignKey(uc => uc.UserId).IsRequired();
                b.HasMany <ApplicationUserLogin>().WithOne().HasForeignKey(ul => ul.UserId).IsRequired();
                b.HasMany <IdentityUserToken <int> >().WithOne().HasForeignKey(ut => ut.UserId).IsRequired();
            });

            builder.Entity <ApplicationRole>(b =>
            {
                b.HasKey(r => r.Id);
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique();
                b.ToTable("EonRoles");
                b.Property(r => r.ConcurrencyStamp).IsConcurrencyToken();

                b.Property(u => u.Name).HasMaxLength(256);
                b.Property(u => u.NormalizedName).HasMaxLength(256);

                b.HasMany <ApplicationUserRole>().WithOne().HasForeignKey(ur => ur.RoleId).IsRequired();
                b.HasMany <IdentityRoleClaim <int> >().WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
            });

            builder.Entity <IdentityRoleClaim <int> >(b =>
            {
                b.HasKey(rc => rc.Id);
                b.ToTable("EonRoleClaims");
            });

            builder.Entity <ApplicationUserRole>(b =>
            {
                b.HasKey(r => new { r.UserId, r.RoleId });
                b.ToTable("EonUserRoles");
            });

            builder.Entity <ApplicationUserClaim>(b =>
            {
                b.HasKey(uc => uc.Id);
                b.ToTable("EonUserClaims");
            });

            builder.Entity <ApplicationUserLogin>(b =>
            {
                b.HasKey(l => new { l.LoginProvider, l.ProviderKey });

                if (maxKeyLength > 0)
                {
                    b.Property(l => l.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(l => l.ProviderKey).HasMaxLength(maxKeyLength);
                }

                b.ToTable("EonUserLogins");
            });

            builder.Entity <IdentityUserToken <int> >(b =>
            {
                b.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });

                if (maxKeyLength > 0)
                {
                    b.Property(t => t.LoginProvider).HasMaxLength(maxKeyLength);
                    b.Property(t => t.Name).HasMaxLength(maxKeyLength);
                }

                if (encryptPersonalData)
                {
                    var tokenProps = typeof(IdentityUserToken <int>).GetProperties().Where(
                        prop => Attribute.IsDefined(prop, typeof(ProtectedPersonalDataAttribute)));
                    foreach (var p in tokenProps)
                    {
                        if (p.PropertyType != typeof(string))
                        {
                            throw new InvalidOperationException();
                        }
                        b.Property(typeof(string), p.Name).HasConversion(converter);
                    }
                }

                b.ToTable("EonUserTokens");
            });
        }