예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var jwtSection = Configuration.GetSection("JWT")
                             ?? throw new ArgumentException("JWT-константы не определены");

            //Внедрение зависимости - JWT-константы из appsettings.json
            services.Configure <JWT>(jwtSection);

            services.AddControllers();
            services.AddMemoryCache();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "Payment System API",
                    Description = "Реализация API для платёжной системы, которая имитирует процесс оплаты банковской картой."
                });
            });

            //Внедрение зависимостей - сервисы
            services.AddSingleton <Cache.ICache, Cache.Cache>();
            services.AddSingleton <Interfaces.IPaymentService, Services.PaymentService>();
            services.AddSingleton <Interfaces.IAccountService, Services.AccountService>();
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            //Конфигурация аутентификации
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer       = jwtSection.GetValue <string>("Issuer"),
                    ValidAudience     = jwtSection.GetValue <string>("Audience"),
                    ValidateLifetime  = true,
                    LifetimeValidator = JWT.LifeTimeValidator,
                    IssuerSigningKey  = JWT.GetSecurityKey(jwtSection.GetValue <string>("Key"))
                };
            });
        }