/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services">The services.</param> public void ConfigureServices(IServiceCollection services) { var connPayRol = _env.IsDevelopment() ? Configuration.GetConnectionString("ENVIRONMENT_ZOO_CONECTION") : Environment.GetEnvironmentVariable("ENVIRONMENT_ZOO_CONECTION"); services.AddCors(c => { c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); services.AddControllers().AddNewtonsoftJson(); services.AddOData(); SetOutputFormatters(services); AppSettingsDto appSettings; if (_env.IsDevelopment()) { var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure <AppSettingsDto>(appSettingsSection); appSettings = appSettingsSection.Get <AppSettingsDto>(); } else { services.Configure <AppSettingsDto>(Configuration); appSettings = Configuration.Get <AppSettingsDto>(); } services.AddDbContext <ZooContext>(options => options.UseNpgsql(connPayRol)); services.AddAutoMapper(typeof(ZooProfileMap)); #region Scope manager services.AddScoped <IAnimalManager, AnimalManager>(); services.AddScoped <ITypeAnimalManager, TypeAnimalManager>(); #endregion #region Scope repository services.AddScoped <IGetData <Animal>, GetRepository <Animal> >(); services.AddScoped <ICrudBasic <Animal>, CrudRepository <Animal> >(); services.AddScoped <IGetData <AnimalType>, GetRepository <AnimalType> >(); #endregion var secret = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthorization(options => { var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme); defaultAuthorizationPolicyBuilder = defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser(); options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build(); }); 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(secret), ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = appSettings.Issuer, ValidAudience = appSettings.Audience, ClockSkew = TimeSpan.Zero }; }); services.AddSwaggerGen(Swagger => { Swagger.EnableAnnotations(); Swagger.OperationFilter <ODataQueryOptionsFilter>(); Swagger.AddServer(new OpenApiServer() { Url = appSettings.UrlServerSwagger, Description = "Local development server" }); Swagger.SwaggerDoc("v1", new OpenApiInfo { Version = "v1.0.0", Title = "Zoo Web Api", Description = "It allows manage informarion of the Zoo.", Contact = new OpenApiContact() { Name = "Ing. Victor A. Echeverria", Email = "*****@*****.**", Url = new Uri("https://www.google.com/"), } }); Swagger.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { In = ParameterLocation.Header, Description = "JWT authorization header using Bearer schema", Name = "Authorization", Type = SecuritySchemeType.ApiKey }); Swagger.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] { } } }); var basePath = _env.ContentRootPath; var xmlPath = Path.Combine(basePath, "ApiZoo.xml"); var xmlDtoPath = Path.Combine(basePath, "ZooDto.xml"); var xmlAuthPath = Path.Combine(basePath, "ZooService.xml"); Swagger.IncludeXmlComments(xmlPath); Swagger.IncludeXmlComments(xmlDtoPath); Swagger.IncludeXmlComments(xmlAuthPath); }); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services">The services.</param> public void ConfigureServices(IServiceCollection services) { var conUser = _env.IsDevelopment() ? Configuration.GetConnectionString("ENVIRONMENT_USER_CONECTION") : Environment.GetEnvironmentVariable("ENVIRONMENT_USER_CONECTION"); services.AddCors(c => { c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); }); services.AddControllers(); AppSettingsDto appSettings; if (_env.IsDevelopment()) { var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure <AppSettingsDto>(appSettingsSection); appSettings = appSettingsSection.Get <AppSettingsDto>(); } else { services.Configure <AppSettingsDto>(Configuration); appSettings = Configuration.Get <AppSettingsDto>(); } services.AddIdentity <ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores <SecurityContext>() .AddDefaultTokenProviders(); services.AddDbContext <SecurityContext>(options => options.UseNpgsql(conUser)); services.AddScoped <IApplicationUserManager, ApplicationUserManager>(); services.AddSwaggerGen(Swagger => { Swagger.EnableAnnotations(); Swagger.AddServer(new OpenApiServer() { Url = appSettings.UrlServerSwagger, Description = "Local development server" }); Swagger.SwaggerDoc("v1", new OpenApiInfo { Version = "v1.0.0", Title = "Web Api Authentication", Description = "JWT token authentication API.", Contact = new OpenApiContact() { Name = "Ing. Victor A. Echeverria", Email = "*****@*****.**", Url = new System.Uri("https://www.google.com/"), } }); var basePath = _env.ContentRootPath; var xmlPath = Path.Combine(basePath, "ApiSecurity.xml"); var xmlDtoPath = Path.Combine(basePath, "SecurityDto.xml"); var xmlAuthPath = Path.Combine(basePath, "SecurityService.xml"); Swagger.IncludeXmlComments(xmlPath); Swagger.IncludeXmlComments(xmlDtoPath); Swagger.IncludeXmlComments(xmlAuthPath); }); }