Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            var builder = services.AddIdentityServer()
                          .AddInMemoryIdentityResources(InMemoryConfig.GetIdentityResources())
                          .AddInMemoryApiScopes(InMemoryConfig.GetApiScopes())
                          .AddInMemoryClients(InMemoryConfig.GetClients())
                          .AddTestUsers(InMemoryConfig.GetUsers());

            builder.AddDeveloperSigningCredential();

            services.AddAuthentication()
            .AddOpenIdConnect("oidc", "Demo IdentityServer", options =>
            {
                options.SignInScheme  = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                options.SignOutScheme = IdentityServerConstants.SignoutScheme;
                options.SaveTokens    = true;

                options.Authority    = "https://demo.identityserver.io/";
                options.ClientId     = "interactive.confidential";
                options.ClientSecret = "cepres";
                options.ResponseType = "code";

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name",
                    RoleClaimType = "role"
                };
            });
        }
Ejemplo n.º 2
0
        public static IHost MigrateDatabase(this IHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();

                using (var context = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>())
                {
                    try
                    {
                        context.Database.Migrate();

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

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

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

                            context.SaveChanges();
                        }

                        if (!context.ApiResources.Any())
                        {
                            foreach (var resource in InMemoryConfig.GetApiResources())
                            {
                                context.ApiResources.Add(resource.ToEntity());
                            }
                            context.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        //Log errors or do anything you think it's needed
                        throw;
                    }
                }
            }

            return(host);
        }
 public static IIdentityServerBuilder AddInMemoryConfiguration(this IIdentityServerBuilder builder, IConfigurationSection config)
 {
     return builder
         .AddInMemoryIdentityResources(InMemoryConfig.GetIds(config.GetSection("Ids")))
         .AddInMemoryApiResources(InMemoryConfig.GetApis(config.GetSection("Apis")))
         .AddInMemoryApiScopes(InMemoryConfig.GetApiScopes(config.GetSection("Apis")))
         .AddInMemoryClients(InMemoryConfig.GetClients(config.GetSection("Clients")));
 }
Ejemplo n.º 4
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentityServer()
            .AddInMemoryApiScopes(InMemoryConfig.GetApiScopes())
            .AddInMemoryApiResources(InMemoryConfig.GetApiResources())
            .AddInMemoryIdentityResources(InMemoryConfig.GetIdentityResources())
            .AddTestUsers(InMemoryConfig.GetUsers())
            .AddInMemoryClients(InMemoryConfig.GetClients())
            .AddDeveloperSigningCredential();

            services.AddControllersWithViews();
        }
Ejemplo n.º 5
0
        private static void InitializeConfigurationDbContext(IConfiguration seedDataConfig, IServiceScope serviceScope)
        {
            serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();

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

            context.Database.Migrate();

            if (seedDataConfig == null)
            {
                return;
            }

            if (!context.Clients.Any())
            {
                foreach (var client in InMemoryConfig.GetClients(seedDataConfig.GetSection("Clients")))
                {
                    context.Clients.Add(client.ToEntity());
                }

                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                foreach (var resource in InMemoryConfig.GetIds(seedDataConfig.GetSection("Ids")))
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }

                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                foreach (var resource in InMemoryConfig.GetApis(seedDataConfig.GetSection("Apis")))
                {
                    context.ApiResources.Add(resource.ToEntity());
                }

                foreach (var resource in InMemoryConfig.GetApiScopes(seedDataConfig.GetSection("Apis")))
                {
                    context.ApiScopes.Add(resource.ToEntity());
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 6
0
 private static void AddIdentityServer(IServiceCollection services, IWebHostEnvironment environment)
 {
     if (environment.IsEnvironment("Test"))
     {
         services.AddIdentityServer()
         .AddApiAuthorization <ApplicationUser, ApplicationDbContext>()
         //api resources
         .AddInMemoryApiResources(InMemoryConfig.GetApiResources())
         .AddInMemoryApiScopes(InMemoryConfig.GetApiScopes())
         .AddTestUsers(InMemoryConfig.Users().ToList())
         .AddInMemoryIdentityResources(InMemoryConfig.GetIdentityResources())
         .AddInMemoryClients(InMemoryConfig.GetClients());
     }
     else
     {
         services.AddIdentityServer()
         .AddApiAuthorization <ApplicationUser, ApplicationDbContext>();
     }
 }
Ejemplo n.º 7
0
        private static void EnsureSeedData(ConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients 正在初始化");
                foreach (var client in InMemoryConfig.GetClients())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.IdentityResources.Any())
            {
                Console.WriteLine("IdentityResources 正在初始化");
                foreach (var resource in InMemoryConfig.GetIdentityResources())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources 正在初始化");
                foreach (var resource in InMemoryConfig.GetApiResources())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }

            if (!context.ApiScopes.Any())
            {
                Console.WriteLine("ApiScopes 正在初始化");
                foreach (var resource in InMemoryConfig.GetApiScopes())
                {
                    context.ApiScopes.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }
Ejemplo n.º 8
0
        private void FillIs4DataBase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                if (serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.EnsureCreated())
                {
                    try
                    {
                        var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();
                        context.Database.Migrate();
                        if (!context.Clients.Any())
                        {
                            foreach (var client in InMemoryConfig.GetClients())
                            {
                                context.Clients.Add(client.ToEntity());
                            }
                            context.SaveChanges();
                        }
                        if (!context.IdentityResources.Any())
                        {
                            foreach (var resource in InMemoryConfig.GetIdentityResources())
                            {
                                context.IdentityResources.Add(resource.ToEntity());
                            }
                            context.SaveChanges();
                        }
                        if (!context.ApiScopes.Any())
                        {
                            foreach (var apiScope in InMemoryConfig.GetApiScopes())
                            {
                                context.ApiScopes.Add(apiScope.ToEntity());
                            }
                            context.SaveChanges();
                        }
                        if (!context.ApiResources.Any())
                        {
                            foreach (var resource in InMemoryConfig.GetApiResources())
                            {
                                context.ApiResources.Add(resource.ToEntity());
                            }
                            context.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }

                    try
                    {
                        var manager     = serviceScope.ServiceProvider.GetRequiredService <SignInManager <IdentityUser> >();
                        var usercontext = serviceScope.ServiceProvider.GetRequiredService <IdentityUsersContext>();
                        usercontext.Database.Migrate();
                        if (!usercontext.Users.Any())
                        {
                            foreach (var user in InMemoryConfig.GetUsers())
                            {
                                var newUser = new IdentityUser
                                {
                                    UserName = user.Username,
                                    Id       = user.SubjectId
                                };
                                newUser.PasswordHash = new PasswordHasher <IdentityUser>().HashPassword(newUser, user.Password);
                                var task = Task.Run(async() => { await manager.UserManager.CreateAsync(newUser); });
                                task.Wait();
                                foreach (var claim in user.Claims)
                                {
                                    usercontext.UserClaims.Add(
                                        new IdentityUserClaim <string>
                                    {
                                        ClaimType  = claim.Type,
                                        ClaimValue = claim.Value,
                                        UserId     = user.SubjectId
                                    });
                                    usercontext.SaveChanges();
                                }
                            }
                        }
                        if (!usercontext.UserRoles.Any())
                        {
                            usercontext.Roles.Add(new IdentityRole("manager"));
                            usercontext.Roles.Add(new IdentityRole("developer"));
                            usercontext.SaveChanges();
                        }
                        //if (!usercontext.UserClaims.Any())
                        //{
                        //    foreach (var user in InMemoryConfig.GetUsers())
                        //    {

                        //    }
                        //    usercontext.SaveChanges();
                        //}
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }