using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text; namespace Net.Business.Services { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; Net.Data.LocalData.Configure(configuration); } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.ConfigureCors(); services.ConfigureIISIntegration(); services.ConfigureSQLConnection(); services.ConfigureHttpClientServiceLayer(); /*Linea con el cual obtenemos datos de la cabecera*/ services.AddSingleton(); //Autenticacion string semilla = Configuration.GetSection("ParametrosTokenConfig").GetValue("Semilla"); string emisor = Configuration.GetSection("ParametrosTokenConfig").GetValue("Emisor"); string destinatario = Configuration.GetSection("ParametrosTokenConfig").GetValue("Destinatario"); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(semilla)); services.AddAuthentication (JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(opt => { opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidIssuer = emisor, ValidAudience = destinatario, IssuerSigningKey = key }; }); services.ConfigureRepositoryWrapper(); /*De aqui en adelante configuracion de documentacion de nuestra API*/ services.AddSwaggerGen(options => { options.SwaggerDoc("ApiSAPWMS", new Microsoft.OpenApi.Models.OpenApiInfo() { Title = "API SAP - WMS", Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(), Description = "API Integración SAP / WMS", Contact = new Microsoft.OpenApi.Models.OpenApiContact() { Name = "IT Consultores Asociados", }, License = new Microsoft.OpenApi.Models.OpenApiLicense() { Name = "MIT License", } }); var archivoXmlComentarios = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var rutaApiComentarios = Path.Combine(AppContext.BaseDirectory, archivoXmlComentarios); options.IncludeXmlComments(rutaApiComentarios); /*Primero definir el esquema de seguridad*/ options.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme { Description = "Autenticacion JWT (Bearer)", Type = Microsoft.OpenApi.Models.SecuritySchemeType.Http, Scheme = "bearer" }); options.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement { { new Microsoft.OpenApi.Models.OpenApiSecurityScheme { Reference = new Microsoft.OpenApi.Models.OpenApiReference { Id = "Bearer", Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme } }, new List() } }); }); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.ConfigureExceptionHandler(); //Linea para documentacion api app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("swagger/ApiSAPWMS/swagger.json", "API SAP - WMS"); options.RoutePrefix = ""; }); app.UseRouting(); app.UseCors("CorsPolicy"); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }