Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IBaseURIs>(Configuration.GetSection("BaseURIs").Get <BaseURIs>());
            var baseuri = new BaseURIs();

            Configuration.GetSection(nameof(BaseURIs)).Bind(baseuri);

            services.AddControllersWithViews();
            services.AddHttpClient("GOSDEFAULTGATEWAY", client =>
            {
                client.BaseAddress = new Uri(baseuri.LiveGateway);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            });
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
        }
        public void InstallServices(IServiceCollection services, IConfiguration configuration)
        {
            var jwtSettings = new JwtSettings();

            configuration.Bind(nameof(jwtSettings), jwtSettings);

            services.AddSingleton(jwtSettings);

            var tokenValidatorParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)),
                ValidateIssuer           = false,
                ValidateAudience         = false,
                RequireExpirationTime    = false,
                ValidateLifetime         = true,
            };

            services.AddSingleton(tokenValidatorParameters);

            services.AddScoped <IUriService, UriService>();
            services.AddScoped <IIdentityService, IdentityService>();
            services.AddSingleton <ILoggerService, LoggerService>();
            services.AddTransient <IInternetService, InternetService>();
            services.AddScoped <IPPEServerRequest, PPEServerRequest>();

            services.AddSingleton <IBaseURIs>(configuration.GetSection("BaseURIs").Get <BaseURIs>());

            services.AddMvc(options =>
            {
                options.EnableEndpointRouting = false;
                options.Filters.Add <ValidationFilter>();
            })
            .AddFluentValidation(mvcConfuguration => mvcConfuguration.RegisterValidatorsFromAssemblyContaining <Startup>())
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddCors(options =>
            {
                options.AddPolicy(MyAllowSpecificOrigins,
                                  builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            var baseuri = new BaseURIs();

            configuration.GetSection(nameof(BaseURIs)).Bind(baseuri);

            services.AddHttpClient("GOSDEFAULTGATEWAY", client =>
            {
                client.BaseAddress = new Uri(baseuri.LiveGateway);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            });


            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.SaveToken = true;
                x.TokenValidationParameters = tokenValidatorParameters;
            });
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IActionContextAccessor, ActionContextAccessor>();
            services.AddSingleton <IUriService>(provider =>
            {
                var accessor    = provider.GetRequiredService <IHttpContextAccessor>();
                var request     = accessor.HttpContext.Request;
                var absoluteUrl = string.Concat(request.Scheme, "://", request.Host.ToUriComponent(), "/");
                return(new UriService(absoluteUrl));
            });
            services.AddSwaggerGen(x =>
            {
                x.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title          = "Purchases and Payables Cloud",
                    Version        = "V1",
                    Description    = "An API to perform business automated operations",
                    TermsOfService = new Uri("http://www.godp.co.uk/"),
                    Contact        = new OpenApiContact
                    {
                        Name  = "GODP Tech",
                        Email = "*****@*****.**",
                        Url   = new Uri("https://twitter.com/FavourE65881201"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "GODP API LICX",
                        Url  = new Uri("http://www.GODP.co.uk/"),
                    },
                });

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

                var security = new Dictionary <string, IEnumerable <string> >
                {
                    { "Bearer", new string[0] }
                };
                x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "GODP Cloud Authorization header using bearer scheme",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                });
                x.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme {
                            Reference = new OpenApiReference
                            {
                                Id = "Bearer", Type = ReferenceType.SecurityScheme
                            }
                        }, new List <string>()
                    }
                });
            });
        }