示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var pfxFilePath     = Configuration.GetSection("Certificate:PfxFilePath");
            var pfxFilePassword = Configuration.GetSection("Certificate:Password");

            services.AddIdentityServer()
            .AddSigningCredential(new X509Certificate2(pfxFilePath.Value, pfxFilePassword.Value))
            .AddInMemoryClients(Clients.Get())
            .AddInMemoryApiResources(ApiRecourses.Get())
            .AddTestUsers(TestUsers.Get().ToList());

            services.AddMvc();
        }
示例#2
0
        private static void InitializeDbTestData(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();
                serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>().Database.Migrate();
                serviceScope.ServiceProvider.GetRequiredService <ApplicationDbContext>().Database.Migrate();

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

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

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

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

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

                var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <User> >();
                if (!userManager.Users.Any())
                {
                    foreach (var testUser in TestUsers.Get())
                    {
                        User identityUser = new User()
                        {
                            Id       = testUser.SubjectId,
                            UserName = testUser.Username
                        };

                        userManager.CreateAsync(identityUser, testUser.Password).Wait();
                        userManager.AddClaimsAsync(identityUser, testUser.Claims.ToList()).Wait();
                    }
                }
            }
        }