// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(c => c.AddDefaultPolicy(builder => {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            services.AddControllers();

            var InfoSwagger = new Microsoft.OpenApi.Models.OpenApiInfo
            {
                Version     = "v1",
                Title       = "Api Core Restfull CRUD",
                Description = "A simple example ASP.NET Core Web API Rest With Dapper And EF, Created By Rafael Hueb",
                Contact     = new OpenApiContact()
                {
                    Name  = "Rafael Hueb",
                    Email = "*****@*****.**",
                    Url   = new Uri("https://github.com/rafaelhueb92")
                }
            };

            new SwaggerServices(services).ConfigureSwagger("V1", InfoSwagger);
        }
        public static IServiceCollection AddSwaggerService(this IServiceCollection services)
        {
            var serviceTitle       = Assembly.GetExecutingAssembly().GetName().Name;
            var serviceDescription = "Multnomah County API";
            var openApiContact     = new OpenApiContact {
                Name = "Team Bravo", Email = "*****@*****.**"
            };


            var openApiInfoV1 = new Microsoft.OpenApi.Models.OpenApiInfo
            {
                Title       = serviceTitle,
                Version     = "V1",
                Description = serviceDescription,
                Contact     = openApiContact
            };

            services.AddSwaggerGen(c =>
            {
                c.EnableAnnotations();
                c.SwaggerDoc("V1", openApiInfoV1);
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = System.IO.Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath, includeControllerXmlComments: true);
                // add security scheme
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Type   = SecuritySchemeType.OAuth2,
                    Scheme = "bearer",
                    Flows  = new OpenApiOAuthFlows
                    {
                        Implicit = new OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri("/connect/authorize", UriKind.Relative),
                            TokenUrl         = new Uri("/connect/token", UriKind.Relative),
                            Scopes           = new Dictionary <string, string>
                            {
                                { "Reader", "Access read operations" },
                                { "Writer", "Access write operations" }
                            }
                        }
                    }
                });;
                // add security requirement
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme {
                            Reference = new OpenApiReference
                            {
                                Id   = "Bearer",
                                Type = ReferenceType.SecurityScheme
                            }
                        }, new List <string>()
                    }
                });
            });
            return(services);
        }
示例#3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            var key = "this is my test key";

            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(Encoding.ASCII.GetBytes(key)),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });
            services.AddSingleton <IJwtAuthenticationManager>(new JwtAuthenticationManager(key));
            //services.AddDbContext
            services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  builder =>
                {
                    builder.WithOrigins("http://localhost:3000", "http://localhost:3001")
                    .AllowAnyHeader()
                    .AllowAnyMethod();;
                });
            });
            OpenApiInfo info = new Microsoft.OpenApi.Models.OpenApiInfo {
                Title = "Examen Osvaldo monzon", Version = "version1"
            };

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Osvaldo Monzon - Examen", Version = "v1"
                });
                c.AddSecurityDefinition("bearer", new OpenApiSecurityScheme
                {
                    Name         = "Authorization",
                    Type         = SecuritySchemeType.ApiKey,
                    Scheme       = "bearer",
                    BearerFormat = "JWT",
                    In           = ParameterLocation.Header,
                    Description  = "JWT Authorization header using the Bearer scheme.",
                });

                //////Add Operation Specific Authorization///////
                c.OperationFilter <AuthOperationFilter>();
                ////////////////////////////////////////////////
            });
        }
示例#4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddPersistence(Configuration);
            services.AddRepository();
            services.AddServices();
            services.AddTokens();
            services.AddControllers();

            var tokenopts = Configuration.GetSection("TokenOptions").Get <TokenOptions>();

            services.Configure <TokenOptions>(Configuration.GetSection("TokenOptions"));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(jwtopt =>
            {
                jwtopt.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                {
                    ValidateAudience         = true,
                    ValidateIssuer           = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = tokenopts.Issuer,
                    ValidAudience    = tokenopts.Audience,
                    IssuerSigningKey = SignHandler.GetSecurityKey(tokenopts.SecurityKey),
                    ClockSkew        = TimeSpan.Zero
                };
            });


            Microsoft.OpenApi.Models.OpenApiInfo inf = new Microsoft.OpenApi.Models.OpenApiInfo();
            inf.Title       = "Authentication API";
            inf.Description = "SWAGGER DOCUMENT";

            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <DomainToResponseProfile>();
            });

            IMapper mapper = config.CreateMapper();

            services.AddSingleton(mapper);
            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
                    In          = ParameterLocation.Header,
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey
                });
                c.OperationFilter <SecurityRequirementsOperationFilter>();

                c.SwaggerDoc("v1", inf);

                var xmlPath = System.AppDomain.CurrentDomain.BaseDirectory + @"Authentication.xml";
                c.IncludeXmlComments(xmlPath);
            });
        }
示例#5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            services.AddControllers();

            var cn = Configuration.GetConnectionString("ApiConnection");

            services.AddDbContext <DataContext>(options =>
                                                options.UseMySQL(cn));

            Injection(services);

            var InfoSwagger = new Microsoft.OpenApi.Models.OpenApiInfo
            {
                Version     = "v1",
                Title       = "Anúncios WebMotors",
                Description = "API do teste de admissão para a WebMotors",
                Contact     = new OpenApiContact()
                {
                    Name  = "Rafael Hueb",
                    Email = "*****@*****.**",
                    Url   = new Uri("http://rafaelhueb.com.s3-website-sa-east-1.amazonaws.com/")
                }
            };

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", InfoSwagger);
            });
        }
示例#6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region Log
            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://localhost:9200/"))
            {
                AutoRegisterTemplate = true,
            })
                         .CreateLogger();
            #endregion

            services.AddResponseCaching(_ =>
            {
                _.MaximumBodySize       = 100;
                _.SizeLimit             = 100;
                _.UseCaseSensitivePaths = false;
            });

            services.AddPersistence(Configuration);
            services.AddRepository();
            services.AddServices();
            services.AddTokens();
            services.AddControllers();



            var tokenopts = Configuration.GetSection("TokenOptions").Get <TokenOptions>();
            services.Configure <TokenOptions>(Configuration.GetSection("TokenOptions"));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(jwtopt =>
            {
                jwtopt.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                {
                    ValidateAudience         = true,
                    ValidateIssuer           = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = tokenopts.Issuer,
                    ValidAudience    = tokenopts.Audience,
                    IssuerSigningKey = SignHandler.GetSecurityKey(tokenopts.SecurityKey),
                    ClockSkew        = TimeSpan.Zero
                };
            });


            Microsoft.OpenApi.Models.OpenApiInfo inf = new Microsoft.OpenApi.Models.OpenApiInfo();
            inf.Title       = "Authentication API";
            inf.Description = "SWAGGER DOCUMENT";

            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <DomainToResponseProfile>();
            });

            IMapper mapper = config.CreateMapper();
            services.AddSingleton(mapper);
            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
                    In          = ParameterLocation.Header,
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey
                });
                c.OperationFilter <SecurityRequirementsOperationFilter>();

                c.SwaggerDoc("v1", inf);

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }