Esempio n. 1
0
        private static void SeedIdentityConfig(IServiceScope scope)
        {
            scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>()
            .Database.Migrate();

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

            context.Database.Migrate();

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

                context.SaveChanges();
            }

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

                context.SaveChanges();
            }

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

                context.SaveChanges();
            }

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

                context.SaveChanges();
            }
        }
Esempio n. 2
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)
        {
            var assembly = typeof(Startup).Assembly.GetName().Name;
            var connectionString = _configuration.GetConnectionString("Default");

            services.AddDbContext<AppIdentityDbContext>(options =>
            {
                //options.UseSqlServer(connectionString);
                options.UseInMemoryDatabase("IdentityDB");
            });

            services.AddIdentity<IdentityUser, IdentityRole>(config =>
                {
                    config.Password.RequiredLength = 4;
                    config.Password.RequireDigit = false;
                    config.Password.RequireNonAlphanumeric = false;
                    config.Password.RequireUppercase = false;
                })
                .AddEntityFrameworkStores<AppIdentityDbContext>()
                .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(config =>
            {
                config.Cookie.Name = "IdentityServer.Cookie";
                config.LoginPath = "/Auth/Login";
                config.LogoutPath = "/Auth/Logout";
                config.AccessDeniedPath = "/Auth/AccessDenied";
            });


            services.AddIdentityServer()
                .AddAspNetIdentity<IdentityUser>()
                // this adds the operational data from DB (codes, tokens, consents)
                //.AddOperationalStore(options =>
                //{
                //    options.ConfigureDbContext = builder =>
                //        builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(assembly));
                //    // this enables automatic token cleanup. this is optional.
                //    options.EnableTokenCleanup = true;
                //    options.TokenCleanupInterval = 30; // interval in seconds
                //})
                //.AddConfigurationStore(options =>
                //{
                //    options.ConfigureDbContext = builder =>
                //        builder.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(assembly));
                //})
                .AddInMemoryIdentityResources(InMemory.GetIdentityResources())
                .AddInMemoryApiResources(InMemory.GetApiResources())
                .AddInMemoryClients(InMemory.GetClients())
                .AddInMemoryApiScopes(InMemory.GetApiScopes())
                .AddDeveloperSigningCredential(); 

            services.AddAuthorization();
            services.AddControllersWithViews();

            //services.AddCors(config =>
            //{
            //    config.AddPolicy("AllowAll", policy =>
            //    {
            //        policy.AllowAnyOrigin();
            //        policy.AllowAnyHeader();
            //        policy.AllowAnyMethod();
            //    });
            //});
        }