예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // CORS
            services.AddCors();

            // JSON
            services
            .AddControllers()
            .AddJsonOptions(options => options.JsonSerializerOptions.WriteIndented = true);

            // Chaine de connexion pour EF - DbContext
            var connectionString = Configuration.GetConnectionString("ServiceEpsiDatabase");

            Console.WriteLine($"Startup, connectionString={connectionString}");
            services.AddDbContext <ServiceEpsiContext>(
                options => options.UseSqlServer(connectionString)
                );

            // JTW

            // Réinitialise les clés pour les Claims:
            // https://mderriey.com/2019/06/23/where-are-my-jwt-claims/
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

            services.AddAuthentication(options =>
            {
                // Identity made Cookie authentication the default.
                // However, we want JWT Bearer Auth to be the default.
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultForbidScheme       = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                // Configure JWT Bearer Auth to expect our security key
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters =
                    new TokenValidationParameters
                {
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = AuthenticationModule.GetPublicKey()
                };
            });

            // Swagger
            // Inject an implementation of ISwaggerProvider with defaulted settings applied
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title       = "WSDatabaseCore API",
                    Version     = "V1",
                    Description = "API de gestion des bases de données en ASP.Net Core Web API 3.0",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Jean-Luc Bompard",
                        Email = "*****@*****.**"
                    }
                });

                // Ajout des commentaires XML
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }