Example #1
0
        public void Configuration(IAppBuilder app)
        {
            app.CreatePerOwinContext(AuthServerDbContext.Create);
            app.CreatePerOwinContext <ApplicationUserManager>(ApplicationUserManager.Create);

            app.Map("/identity", idsrvApp =>
            {
                var options = new IdentityServerOptions();

                IdentityServerConfig.Register(options);

                idsrvApp.UseIdentityServer(options);
            });

            HttpConfiguration httpConfiguration = new HttpConfiguration();

            WebApiConfig.Register(httpConfiguration);

            app.UseWebApi(httpConfiguration);
        }
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var keyDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                                      "IdentityServerExample");

            if (!Directory.Exists(keyDir))
            {
                Directory.CreateDirectory(keyDir);
            }
            var builder = services.AddIdentityServer(config =>
            {
                config.InputLengthRestrictions.Password = int.MaxValue;
            })
                          .AddDeveloperSigningCredential(Path.Combine(keyDir, "keys.rsa"))
                          .AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
                          .AddInMemoryClients(IdentityServerConfig.GetClients())
                          .AddTestUsers(IdentityServerConfig.GetUsers().ToList())
                          .AddProfileService <ProfileService>()
                          .AddResourceOwnerValidator <PasswordValidator>();

            // Add framework services.
            services.AddMvc();
        }
        // 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)
        {
            var connectionString   = Configuration.GetConnectionString("DefaultConnection");
            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            //配置 identityserver
            //1.哪些API可以使用这个authorization server.
            //2.那些客户端Client(应用)可以使用这个authorization server.
            //3.指定可以使用authorization server授权的用户.
            services.AddIdentityServer()
            //.AddDeveloperSigningCredential()
            //.AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
            //.AddInMemoryApiResources(IdentityServerConfig.ApiResources())
            //.AddInMemoryClients(IdentityServerConfig.Clients())
            //.AddTestUsers(IdentityServerConfig.Users().ToList());
            .AddDeveloperSigningCredential()
            .AddTestUsers(IdentityServerConfig.Users().ToList())
            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = builder =>
                                             builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
            })
            // this adds the operational data from DB (codes, tokens, consents)
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = builder =>
                                             builder.UseSqlServer(connectionString,
                                                                  sql => sql.MigrationsAssembly(migrationsAssembly));

                // this enables automatic token cleanup. this is optional.
                options.EnableTokenCleanup   = true;
                options.TokenCleanupInterval = 30;
            });


            services.AddMvc();
        }
        private void InitializeDatabase(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate();

                var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();
                context.Database.Migrate();
                if (!context.Clients.Any())
                {
                    foreach (var client in IdentityServerConfig.Clients())
                    {
                        context.Clients.Add(client.ToEntity());
                    }
                    context.SaveChanges();
                }

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

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