Exemplo n.º 1
0
        static void Data_Seeding_Users(
            UserDbContext userContext,
            TenantDbContext tenantDbContext,
            UserManager <AppUser> userManager,
            ConfigurationDbContext identityserverDbContext,
            OAuthAppOptions options)
        {
            if (!userContext.Roles.Any())
            {
                var roles = typeof(DefaultRoles).GetFields();

                foreach (var role in roles)
                {
                    var roleName = role.GetRawConstantValue().ToString();

                    userContext.Roles.Add(new AppRole
                    {
                        Name             = roleName,
                        NormalizedName   = roleName,
                        ConcurrencyStamp = Guid.NewGuid().ToString()
                    });
                }

                userContext.SaveChanges();
            }

            if (!userContext.Users.Any())
            {
                var roleIds = userContext.Roles.Select(x => x.Id).ToList();

                var tenantIds = tenantDbContext.Tenants.Select(x => x.Id).ToList();

                var user = new AppUser()
                {
                    Email          = options.DefaultUserAccount,
                    UserName       = options.DefaultUserAccount,
                    PasswordHash   = options.DefaultUserPassword,
                    EmailConfirmed = true,
                    ParentUserID   = AppConstant.seedUserId
                };

                var r = AppUserService.CreateUser(AppConstant.seedTenantId,
                                                  userManager,
                                                  userContext,
                                                  user,
                                                  roleIds,
                                                  $"{AppConstant.MicroServiceName}.all",
                                                  tenantIds).Result;

                #region User Clients
                var clientIds = identityserverDbContext.Clients.Select(x => x.Id).ToList();
                foreach (var cid in clientIds)
                {
                    user.Clients.Add(new AspNetUserClient()
                    {
                        ClientId = cid
                    });
                }
                #endregion

                #region User ApiResources
                var apiIds = identityserverDbContext.ApiResources.Select(x => x.Id).ToList();
                foreach (var apiId in apiIds)
                {
                    user.ApiResources.Add(new AspNetUserApiResource()
                    {
                        ApiResourceId = apiId,
                    });
                }
                #endregion

                userContext.SaveChanges();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 数据库初始化
        /// </summary>
        public static void InitializeDatabase(IApplicationBuilder app, OAuthAppOptions options)
        {
            Tenant.AppHostName = Tenant.IdentityServerIssuerUri = options.IdentityServerUri.Authority;

            Tenant.WebSite = options.IdentityServerUri.OriginalString;

            using (var scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                #region PersistedGrantDb
                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();
                #endregion

                var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();

                context.Database.Migrate();

                if (!context.Clients.Any())
                {
                    foreach (var client in GetClients(options.IdentityServerUri))
                    {
                        context.Clients.Add(client.ToEntity());
                    }
                    context.SaveChanges();
                }

                if (!context.IdentityResources.Any())
                {
                    foreach (var resource in GetIdentityResources())
                    {
                        context.IdentityResources.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }

                if (!context.ApiResources.Any())
                {
                    foreach (var resource in GetApiResources())
                    {
                        context.ApiResources.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }

                if (!context.ApiScopes.Any())
                {
                    foreach (var resource in GetApiScopes())
                    {
                        context.ApiScopes.Add(resource.ToEntity());
                    }
                    context.SaveChanges();
                }

                #region TenantDb
                var tenantDbContext = scope.ServiceProvider.GetRequiredService <TenantDbContext>();
                tenantDbContext.Database.Migrate();
                Data_Seeding_Tenants(tenantDbContext);
                #endregion

                #region IdentityDb
                var userContext = scope.ServiceProvider.GetRequiredService <UserDbContext>();
                var userManager = scope.ServiceProvider.GetRequiredService <UserManager <AppUser> >();
                userContext.Database.Migrate();
                Data_Seeding_Users(userContext, tenantDbContext, userManager, context, options);
                #endregion

                #region SDKDb
                var SDKDbContext = scope.ServiceProvider.GetRequiredService <SdkDbContext>();
                SDKDbContext.Database.Migrate();
                #endregion
            }
        }