Exemplo 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.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(_configuration["ConnectionString"]);
            });

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            if (_environment.EnvironmentName != "Offline")
            {
                services.AddDataProtectionWithSqlServerForIdentityService(_configuration);
            }

            services.AddHsts(opts =>
            {
                opts.IncludeSubDomains = true;
                opts.MaxAge            = TimeSpan.FromSeconds(15768000);
            });

            services.AddControllersWithViews();

            var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseSuccessEvents     = true;
            })
                          .AddInMemoryIdentityResources(IdentityResourceData.Resources())
                          .AddInMemoryApiResources(ApiResourceData.Resources())
                          .AddInMemoryApiScopes(ApiScopeData.Resources())
                          .AddInMemoryClients(ClientData.GetClients())
                          .AddOperationalStore(options =>
            {
                options.EnableTokenCleanup = true;
                //The number of records to remove at a time. Defaults to 100.
                options.TokenCleanupBatchSize = 100;
                options.TokenCleanupInterval  = 3600;            //Seconds

                options.ConfigureDbContext = b =>
                {
                    options.ConfigureDbContext = c =>
                                                 c.UseSqlServer(_configuration["ConnectionString"]);
                };
            })
                          .AddAspNetIdentity <ApplicationUser>();

            if (_environment.EnvironmentName != "Offline")
            {
                builder.AddProductionSigningCredential(_configuration);
            }
            else
            {
                builder.AddDeveloperSigningCredential();
            }
        }