/// <summary> /// Setup swagger for application /// </summary> /// <param name="services">The service where we register swagger</param> /// <param name="apiConfiguration">The swagger configuration</param> public static void AddMaijeDocumentation(this IServiceCollection services) { var configuration = services.BuildServiceProvider().GetRequiredService <IOptions <MaijeConfiguration> >().Value; services.AddSwaggerGen(options => { if (configuration.OAuth2Enabled) { ConfigureAuthentication(options, configuration.Authentication); } // resolve the IApiVersionDescriptionProvider service // note: that we have to build a temporary service provider here because one has not been created yet var provider = services.BuildServiceProvider().GetRequiredService <IApiVersionDescriptionProvider>(); // add a swagger document for each discovered API version // note: you might choose to skip or document deprecated API versions differently foreach (var description in provider.ApiVersionDescriptions) { options.DescribeAllParametersInCamelCase(); options.SwaggerDoc(description.GroupName, SwaggerHelper.CreateInfoForApiVersion(description, configuration)); } options.CustomSchemaIds(SwaggerHelper.GetCustomSchemaId); // Configure AllOf property of the swagger.json options.SchemaFilter <SubtypeOfAttributeFilter>(); // add a custom operation filter which sets default values options.OperationFilter <DefaultOperationsFilter>(); // workaround with ApiExplorer who don't lower case controller name options.DocumentFilter <LowercaseDocumentFilter>(); // integrate xml comments options.IncludeXmlComments(SwaggerHelper.GetXmlCommentsFilePath()); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Loading AutoMapper Profile var config = new AutoMapper.MapperConfiguration(cfg => { cfg.AddProfile(new GarbProfile()); }); var mapper = config.CreateMapper(); // Adding CORS services.AddCors(o => o.AddPolicy("CorsPolicy", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); })); // Adding authentication services services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Issuer"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])) }; }); // Add framework services. services.AddDbContext <GarbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("GarbDatabase"))); //.AddUnitOfWork<GarbContext>(); services.AddAutoMapper(); services.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV"); services.AddMvc(); services.AddMvcCore().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // In production, the Angular files will be served from this directory services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; }); services.AddApiVersioning(o => { o.AssumeDefaultVersionWhenUnspecified = true; o.DefaultApiVersion = new ApiVersion(1, 0); }); services.AddSwaggerGen(options => { var provider = services.BuildServiceProvider() .GetRequiredService <IApiVersionDescriptionProvider>(); foreach (var description in provider.ApiVersionDescriptions) { options.SwaggerDoc(description.GroupName, SwaggerHelper.CreateInfoForApiVersion(description)); } // add a custom operation filter which sets default values options.OperationFilter <SwaggerDefaultValues>(); // integrate xml comments options.IncludeXmlComments(SwaggerHelper.XmlCommentsFilePath); }); // Added - uses IOptions<T> for your settings. services.AddOptions(); // Added - Confirms that we have a home for our DemoSettings services.Configure <StorageHelper>(Configuration.GetSection("StorageHelperSettings")); services.Configure <LdapHelper>(Configuration.GetSection("Authentication")); //call this in case you need aspnet-user-authtype/aspnet-user-identity //services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); }
public void ConfigureServices(IServiceCollection services) { services.AddSingleton(redisConfiguration); services.AddSingleton(urlBuilderConfiguration); services.AddSingleton(databaseConfiguration); // ** Authentication ** // services.AddAuthentication("Bearer") .AddJwtBearer(options => { options.Authority = urlBuilderConfiguration.Auth.Url; options.Audience = "wpc-netflix-apis"; options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name", RoleClaimType = "role" }; if (this.environment.IsDevelopment()) { options.RequireHttpsMetadata = false; } }); services.AddMongoDb(); services .AddDataProtection() .SetApplicationName("wpc-netflix") .PersistKeysToRedis(redisConfiguration.Connection, "my-dataProtection-keys"); services.AddDistributedRedisCache(options => { options.Configuration = this.redisConfiguration.ConfigurationOptions.ToString(); }); // add the versioned api explorer, which also adds IApiVersionDescriptionProvider service // note: the specified format code will format the version as "'v'major[.minor][-status]" services .AddMvcCore() .AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV"); services.AddMvc(); services.AddApiVersioning(o => o.ReportApiVersions = true); if (!this.environment.IsProduction()) { services.AddSwaggerGen( options => { // resolve the IApiVersionDescriptionProvider service // note: that we have to build a temporary service provider here because one has not been created yet var provider = services.BuildServiceProvider().GetRequiredService <IApiVersionDescriptionProvider>(); // add a swagger document for each discovered API version // note: you might choose to skip or document deprecated API versions differently foreach (var description in provider.ApiVersionDescriptions) { options.SwaggerDoc(description.GroupName, SwaggerHelper.CreateInfoForApiVersion(description)); } // add a custom operation filter which sets default values options.OperationFilter <SwaggerDefaultValues>(); ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; // integrate xml comments // options.IncludeXmlComments(SwaggerHelper.XmlCommentsFilePath); // Authorization API documentation options.OperationFilter <AuthResponsesOperationFilter>(); options.OperationFilter <BadRequestResponseOperationFilter>(); options.OperationFilter <TagByApiExplorerSettingsOperationFilter>(); // Authentication var oAuth2Scheme = new OAuth2Scheme(); oAuth2Scheme.AuthorizationUrl = urlBuilderConfiguration.Auth.Url + "/connect/authorize"; oAuth2Scheme.Flow = "implicit"; oAuth2Scheme.TokenUrl = urlBuilderConfiguration.Auth.Url + "/connect/authorize"; oAuth2Scheme.Scopes = new ConcurrentDictionary <string, string>(); oAuth2Scheme.Scopes.Add(new KeyValuePair <string, string>("admin", "Admin APIs access.")); oAuth2Scheme.Scopes.Add(new KeyValuePair <string, string>("www", "Www APIs access.")); options.AddSecurityDefinition("Bearer", oAuth2Scheme); }); } }