Пример #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Dum: se agrega la inyeccion para el logger
            //services.AddSingleton<ILoggerManager, LoggerManager>();
            services.ConfigureLoggerService();

            //Dum: se realiza una clase que contiene la configuración y se inyecta la configuración para base de datos.
            //services.ConfiguracionSqlContext(Configuration);

            services.AddDbContext <NatilleraDBContext>(opcion =>
                                                       opcion.UseSqlServer(Configuration.GetValue <string>("ConnectionString:DataBaseConexion")));


            //Dum: manejo del token.
            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <NatilleraDBContext>()
            .AddDefaultTokenProviders();

            //Dum: se inyecta el contenedor del repositorio
            //services.ConfiguracionRepositoryContenedor();

            //Dum: se injectan los servicios de las capas de repositorio y servicios.
            ServiceExtensions.AddResgistro(services);

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            //DUM: Inicio configuración swagger, Install-Package Swashbuckle.AspNetCore
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version        = "v1",
                    Title          = "Swagger Api Natillera",
                    Description    = "Servicio para la administración de la información de natilleras",
                    TermsOfService = "No Aplica",
                    Contact        = new Contact()
                    {
                        Name = "Talking Dotnet", Email = "*****@*****.**", Url = "www.talkingdotnet.com"
                    }
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.DocInclusionPredicate((docName, description) => true);
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                c.IncludeXmlComments(xmlPath);
            });
            //DUM: Final Configuración Swagger
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //Dum: se agrega la inyeccion para el logger
            //services.AddSingleton<ILoggerManager, LoggerManager>();
            services.ConfigureLoggerService();

            services.AddDbContext <NatilleraDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DataBaseConexion")));

            services.AddControllers();

            //Dum: manejo del token.
            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <NatilleraDBContext>()
            .AddDefaultTokenProviders();

            //Dum: se inyecta el contenedor del repositorio
            //services.ConfiguracionRepositoryContenedor();

            //Dum: se injectan los servicios de las capas de repositorio y servicios.
            ServiceExtensions.AddResgistro(services);

            //json web token configuracion
            //se realiza la validacion para saber si el token enviado es correcto y
            //poder entrar a la aplicación.
            services.AddAuthentication(option =>
            {
                option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                option.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(opcion =>
                            opcion.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer           = true,
                ValidateAudience         = true,
                ValidateLifetime         = true,
                ValidateIssuerSigningKey = true,
                ValidIssuers             = new List <string> {
                    "yourdomain.com"
                },
                ValidAudience    = "yourdomain.com",
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["va_clave_super_secreta"])),
                ClockSkew        = TimeSpan.Zero
            });
            //fin json web token

            //se instala el swagger: Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc2
            // Register the Swagger generator, defining 1 or more Swagger documents
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(configuracion =>
            {
                configuracion.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version        = "v1",
                    Title          = "ToDo API",
                    Description    = "Natillera ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact        = new OpenApiContact
                    {
                        Name  = "Shayne Boyer",
                        Email = string.Empty,
                        Url   = new Uri("https://twitter.com/spboyer"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url  = new Uri("https://example.com/license"),
                    }
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                configuracion.IncludeXmlComments(xmlPath);

                //configurar ventana para pedir token en openapi
                configuracion.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"
                });

                configuracion.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        new List <string>()
                    }
                });
            });
        }