using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Domain.Ports;
using Swashbuckle.AspNetCore.Swagger;
using WebApi.Helpers;
using WebApi.Security;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace WebApi
{
public class Startup
{
private readonly Wiring.Wiring _wiring;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
// Use the system library for HttpConnections (with systemproxy)
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
Configuration = configuration;
_wiring = new Wiring.Wiring();
}
public void ConfigureServices(IServiceCollection services)
{
JwtTokenAuthentication.Register(services, Configuration);
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.Build());
});
services.AddMvc();
if (Configuration["ApiSettings:DocumentationEnabled"].Contains("true", StringComparison.OrdinalIgnoreCase)) {
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = $"{Configuration["ApiSettings:Title"]} {AppHelper.GetVersion()}",
Version = "v1",
Description = Configuration["ApiSettings:Description"],
Contact = new Contact()
{
Name = Configuration["ApiSettings:ContactName"],
Email = Configuration["ApiSettings:ContactEmail"],
Url = Configuration["ApiSettings:ContactUrl"]
}
});
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.XML";
if (File.Exists(xmlFile)) {
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
}
else
{
DomainPorts.LogMessageStore.StoreError($"Documentation file not found: {xmlFile}");
}
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
In = "header",
Description = "Please enter JWT with Bearer into field",
Name = "Authorization",
Type = "apiKey",
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
{ "Bearer", Enumerable.Empty<string>() },
});
});
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (Configuration["ApiSettings:DocumentationEnabled"].Contains("true", StringComparison.OrdinalIgnoreCase))
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c => { c.RouteTemplate = "docs/{documentName}/docs.json"; });
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/docs/v1/docs.json", $"{Configuration["ApiSettings:Title"]} v1");
c.RoutePrefix = "docs";
c.DocumentTitle = $"{Configuration["ApiSettings:Title"]} {AppHelper.GetVersion()}";
c.HeadContent = $"<style>{File.ReadAllText("./Content/swagger-styles.css")}</style>";
});
}
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseMvc();
}
}
}
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
212000cookie-checkC# configure swagger