I have an issue with Docker Containers.
I am working on an Web Service targeting ASP.Net Core v.3.1 and it has JWT authentization.
My web service works perfectly in IIS but when i build the image and run in on docker , docker can not load the index.html of Swagger.
The Startup code:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
//httpClient
services.AddHttpClient("httpClient");
//Add services
services.AddScoped<IAuthRepository, AuthRepository>();
services.AddScoped<IgenesysToken, ApiToken>();
services.AddScoped<ICrmControllerService, CControllerService>();
services.AddSingleton<ILoggerManager, LoggerManager>();
services.AddScoped<IGenesysControllerServices, ApiControllerService>();
//adding User from appsettings.json lsit
List<User> users = new List< User>();
Configuration.GetSection(nameof(User)).Bind(users);
services.AddSingleton(users);
services.AddMvc()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = true;
});
//add Memory cache
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
//86399 second same as genesys app send the expire time of token.
options.IdleTimeout = TimeSpan.FromSeconds(86399);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
//swagger configuration
services.AddSwaggerGen(opsions=>
{
opsions.SwaggerDoc("V1",
new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "My API",
Description = "My API comunication with RBAL",
Version = "V1"
});
var fileName = $"{Assembly.GetExecutingAssembly().GetName().Name}.Xml";
var filePath = Path.Combine(AppContext.BaseDirectory, fileName);
opsions.IncludeXmlComments(filePath);
opsions.AddSecurityDefinition("bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Authorization header using the Bearer scheme. Example:"Authorization: bearer {token}"",
Name= "Authorization",
Type= SecuritySchemeType.ApiKey,
BearerFormat = "JWT",
});
var securityRequirement = new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type=ReferenceType.SecurityScheme,
Id="bearer"
},
},
new string[]{}
}
};
opsions.AddSecurityRequirement(securityRequirement);
});
//Jwt configuration
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"], // Issuer value at json
ValidAudience = Configuration["Jwt:Audience"], // audience isue at json
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"])),
ClockSkew = TimeSpan.Zero
};
});
// adding Pilicy admin rights
services.AddAuthorization(config =>
{
config.AddPolicy(Policies.Admin, Policies.AdminPolicy());
config.AddPolicy(Policies.User, Policies.UserPolicy());
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// add swager middleware
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/V1/swagger.json", "API");
//set swaggger as start up page ...
options.RoutePrefix = "";
});
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…