Beispiel #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.AddMvc();

            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            IdentityServerConfigurationHelper helper = new IdentityServerConfigurationHelper(Configuration);

            services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;
            })
            .AddDeveloperSigningCredential()
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = builder =>
                                             builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                                                                  sql => sql.MigrationsAssembly(migrationsAssembly));

                // this enables automatic token cleanup. this is optional.
                options.EnableTokenCleanup   = true;
                options.TokenCleanupInterval = 30;
            })
            .AddConfigurationStore(options =>
                                   options.ConfigureDbContext = builder =>
                                                                builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                                                                                     sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)))
            .AddProfileService <ProfileService>()
            .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>();

            //in-memory
            //.AddInMemoryIdentityResources(helper.IdentityResources)
            //.AddInMemoryClients(helper.Clients)
            //.AddInMemoryApiResources(helper.ApiResources);
            //.AddTestUsers();

            //custom dbcontext containg dev arena users
            services.AddDbContext <DevArenaDbContext>(options =>
                                                      options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddAuthentication().AddGoogle("Google", options =>
            {
                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

                options.ClientId     = "434483408261-55tc8n0cs4ff1fe21ea8df2o443v2iuc.apps.googleusercontent.com";
                options.ClientSecret = "3gcoTrEDPPJ0ukn_aYYT6PWo";
            });

            ConfigureDataRepositories(services);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var certificateConfiguration = Configuration.GetSection(nameof(CertificateConfiguration));
            var fileName = certificateConfiguration.GetValue <string>(nameof(CertificateConfiguration.FileName));
            var password = certificateConfiguration.GetValue <string>(nameof(CertificateConfiguration.Password));

            services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });
            IMapper mapper = mappingConfig.CreateMapper();

            services.AddSingleton(mapper);

            services.AddControllers().AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy          = null);
            services.AddControllersWithViews().AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy = null);
            services.AddAuthorization(options =>
            {
                options.AddPolicy("Administators only", policy => policy.RequireRole("Admin"));
            });

            services.Configure <CookieAuthenticationOptions>(IdentityServerConstants.DefaultCookieAuthenticationScheme, options =>
            {
                options.Cookie.SameSite     = SameSiteMode.Lax;
                options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
                options.Cookie.IsEssential  = true;
            });

            // 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.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.Authority            = "https://localhost:5001";
                options.RequireHttpsMetadata = false;
                options.Audience             = "UserManagement";
            });

            services.AddSendgridEmailSender(Configuration);
            services.AddTransient <IReturnUrlParser, Helpers.ReturnUrlParser>();
            services.AddTransient <IPersistedGrantStore, PersistedGrantStore>();
            services.AddTransient <IInitializationHelper, InitializationHelper>();
            services.Configure <EmailConfiguration>(Configuration.GetSection(nameof(EmailConfiguration)));
            services.Configure <AppConfiguration>(Configuration.GetSection(nameof(AppConfiguration)));


            services.AddCors(setup =>
            {
                setup.AddDefaultPolicy(policy =>
                {
                    policy.AllowAnyHeader();
                    policy.AllowAnyMethod();
                    policy.WithOrigins("http://127.0.0.1:8080",
                                       "http://localhost:8080",
                                       "http://localhost:8082",
                                       "http://localhost:4200",
                                       "https://auth.localservice/");
                    policy.AllowCredentials();
                });
            });

            services.AddIdentityServer(
                options => {
                options.UserInteraction.LoginUrl      = "/Account/Login";
                options.UserInteraction.ErrorUrl      = "/Home/Error";
                options.UserInteraction.LogoutUrl     = "/Account/Logout";
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;
            })
            .AddJwtBearerClientAuthentication()
            .AddInMemoryIdentityResources(IdentityServerConfigurationHelper.GetIdentityResources())
            .AddInMemoryApiResources(IdentityServerConfigurationHelper.GetApis())
            .AddInMemoryClients(IdentityServerConfigurationHelper.GetClients())
            .AddAspNetIdentity <ApplicationUser>()
            .AddSigningCredential(new X509Certificate2(fileName, password));
        }