// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().ConfigureApiBehaviorOptions(options =>
            {
                options.SuppressConsumesConstraintForFormFileParameters = true;
            });

            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            DependencyConfig.RegisterConfig(services);

            services.AddControllers();

            services.AddDbContext <MyCartDBContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));


            var key            = Configuration.GetValue <string>("SecretKey");
            var symmetricToken = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience         = true,
                    ValidateIssuer           = true,
                    ValidateIssuerSigningKey = true,
                    ValidateLifetime         = true,
                    ValidIssuer      = "Admin",
                    ValidAudience    = "User",
                    IssuerSigningKey = symmetricToken
                };
            });


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "MyCart API",
                    Description = "Contains list of all the APIs used in MyCart",
                });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = @"JWT Authorization header using the Bearer scheme.
                      Enter 'Bearer' [space] and then your token in the text input below.
                      Example: 'Bearer your token'",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        new List <string>()
                    }
                });
            });
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Services.Services.TaskScheduler.Instance.ScheduleTask(02, 30, 24,
                                                                  () =>
            {
                new Services.Services.MailService("morning");
            });
            Services.Services.TaskScheduler.Instance.ScheduleTask(14, 30, 24,
                                                                  () =>
            {
                new Services.Services.MailService("evening");
            });
            services.AddControllers();
            DependencyConfig.RegisterConfig(services);
            services.AddCors(o => o.AddPolicy("CabOPolicy", builder =>
            {
                builder.AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin();
            }));
            services.AddDbContext <CabODbContext>(options =>

                                                  options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection")), ServiceLifetime.Transient);

            var key            = "Experion.CabO,CabBookingSystemDevelepedByFreshersBatch";
            var symmetricToken = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience         = true,
                    ValidateIssuer           = true,
                    ValidateIssuerSigningKey = true,
                    ValidateLifetime         = true,
                    ValidIssuer      = "CabOAdmin",
                    ValidAudience    = "CabOUser",
                    IssuerSigningKey = symmetricToken
                };
            });


            services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "CabO",
                    Description = "Cab Booking System"
                });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = @"JWT Authorization header using the Bearer scheme. \r\n\r\n 
                 Enter 'Bearer' [space] and then your token in the text input below.
                 \r\n\r\nExample: 'Bearer ANCDDDDDDDDDDDDKDKJDKWJ'",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        new List <string>()
                    }
                });
            });
            services.AddControllers();
        }