コード例 #1
0
 public static void AddIdentityServerWithDefaults(this IServiceCollection services)
 {
     services.AddIdentityServer()
     .AddInMemoryClients(TestClients.Get())
     .AddInMemoryIdentityResources(TestResources.GetIdentityResources())
     .AddInMemoryApiResources(TestResources.GetApiResources())
     .AddTestUsers(TestUsers.Get())
     .AddDeveloperSigningCredential();
 }
コード例 #2
0
        public static void UseSampleIdentityData(this IApplicationBuilder app)
        {
            using (var scope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();
                scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>().Database.Migrate();
                scope.ServiceProvider.GetRequiredService <ApplicationDbContext>().Database.Migrate();

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

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

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

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

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

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