Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IConfigurationSection jwtAppSettings = Configuration.GetSection(nameof(JwtConfiguration));
            IConfigurationSection authSettings   = Configuration.GetSection(nameof(AuthConfiguration));

            services.AddProtectionData(this.Configuration);

            services.AddSAMLAuth(this.Configuration);
            services.AddSPIDLogging();

            services.AddTransient <CustomJwtBearerEvents>();

            ServiceProvider        sp = services.BuildServiceProvider();
            IDataProtectionService dataProtectionService = sp.GetService <IDataProtectionService>();
            SymmetricSecurityKey   signingKey            = dataProtectionService.GetSigningKey();

            services.AddJwtSPID(this.Configuration, signingKey);
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddCookie()
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, cfg =>
            {
                cfg.RequireHttpsMetadata = true;
                cfg.SaveToken            = true;

                cfg.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = true,
                    ValidIssuer    = jwtAppSettings[nameof(JwtConfiguration.Issuer)],

                    ValidateAudience = true,
                    ValidAudience    = jwtAppSettings[nameof(JwtConfiguration.Issuer)],

                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = signingKey,

                    ValidateLifetime = true,
                    ClockSkew        = TimeSpan.Zero
                };

                cfg.EventsType = typeof(CustomJwtBearerEvents);
            });

            services.AddOptions();
            services.Configure <ClientConfiguration>(options =>
            {
                options.IdpType         = authSettings.GetValue <IdpType>(nameof(AuthConfiguration.IdpType));
                options.ApplicationName = this.Configuration.GetValue <string>(nameof(ClientConfiguration.ApplicationName));
            });

            Assembly             externalAssembly     = typeof(SamlController).GetTypeInfo().Assembly;
            EmbeddedFileProvider embeddedFileProvider = new EmbeddedFileProvider(externalAssembly, "VecompSoftware.DocSuite.SPID.AuthEngine");

            services.Configure <RazorViewEngineOptions>(options =>
            {
                options.FileProviders.Add(embeddedFileProvider);
            });

            IConfigurationSection corsOrigins = Configuration.GetSection("AllowedOrigins");

            string[] allowedOrigins = corsOrigins.Get <string[]>();
            services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigin",
                                  builder => builder
                                  .WithOrigins(allowedOrigins)
                                  .AllowAnyMethod()
                                  .AllowAnyHeader());
            });
            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
            });
        }