Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddHostedService<BackgroundTelegramBotService>();
            services.AddControllersWithViews();

            // configures IIS out-of-proc settings (see https://github.com/aspnet/AspNetCore/issues/14882)
            services.Configure <IISOptions>(iis =>
            {
                iis.AuthenticationDisplayName = "Windows";
                iis.AutomaticAuthentication   = false;
            });

            // configures IIS in-proc settings
            services.Configure <IISServerOptions>(iis =>
            {
                iis.AuthenticationDisplayName = "Windows";
                iis.AutomaticAuthentication   = false;
            });

            services.AddDbContext <ApplicationDbContext>(config =>
            {
                // for in memory database
                config.UseInMemoryDatabase("MemoryBaseDataBase");
            });

            // AddIdentity :-  Registers the services
            services.AddIdentity <AppUser, IdentityRole>(config =>
            {
                // User defined password policy settings.
                config.Password.RequiredLength         = 4;
                config.Password.RequireDigit           = false;
                config.Password.RequireNonAlphanumeric = false;
                config.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            var builder = services.AddIdentityServer(options =>
            {
                options.Csp.AddDeprecatedHeader       = false;
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;
            })
                          .AddRedirectUriValidator <CustomRedirectUriValidator>()
                          .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = builder => builder.UseInMemoryDatabase("MemoryBaseDataBase");
            })
                          .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = builder => builder.UseInMemoryDatabase("MemoryBaseDataBase");
                options.EnableTokenCleanup = true;
            })
                          .AddAspNetIdentity <AppUser>()
                          .AddProfileService <ProfileService>();


            // in-memory, code config
            builder.AddInMemoryIdentityResources(Config.Ids);
            builder.AddInMemoryApiResources(Config.Apis);
            builder.AddInMemoryClients(Config.Clients);

            // Generate 6000 random users
            var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
            var sc          = services.BuildServiceProvider();
            var userManager = sc.GetService <UserManager <AppUser> >();
            var testUser    = new TestUsers(userManager);

            Task.Run(() => testUser.AddRandomUsersToTheUserStore());


            // not recommended for production - you need to store your key material somewhere secure
            builder.AddDeveloperSigningCredential();
            services.AddApplicationInsightsTelemetry(_config["ApplicationInsights_InstrumentationKey"]);
        }