// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvcCore().AddApiExplorer(); services.AddCors(); // Register the Swagger generator, defining 1 or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); 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 12345abcdef\"", 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>() } }); }); var mongoDbSection = Configuration.GetSection("Connections"); var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure <MongoSettings>(mongoDbSection); services.Configure <AppSettings>(appSettingsSection); services.AddScoped <IMongoClient>(x => new MongoClient(mongoDbSection.GetValue <string>("ConnectionString"))); services.AddScoped <IProductQueries, ProductQueries>(); services.AddScoped <IProductRepository, ProductRepository>(); services.AddScoped(typeof(ICollectionRepository <>), typeof(CollectionRepository <>)); services.AddScoped <IProductService, ProductService>(); services.AddScoped <IMapper>(x => new Mapper(AutoMapperConfig.GetConfiguration())); services.AddScoped <IUserService, UserService>(); services.AddScoped <IUserQueries, UserQueries>(); // configure jwt authentication var appSettings = appSettingsSection.Get <AppSettings>(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); services.AddAuthorization(options => { options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build(); }); }
protected void Application_Start() { AutoMapperConfig.Initialize(); GlobalConfiguration.Configure(WebApiConfig.Register); }