106 lines
3.3 KiB
C#
106 lines
3.3 KiB
C#
using DamageAssesment.Api.Questions.Db;
|
|
using DamageAssesment.Api.Questions.Interfaces;
|
|
using DamageAssesment.Api.Questions.Providers;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
|
|
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
|
|
}));
|
|
// Add services to the container.
|
|
var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey");
|
|
builder.Services.AddAuthentication(item =>
|
|
{
|
|
item.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
item.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
}).AddJwtBearer(item =>
|
|
{
|
|
item.RequireHttpsMetadata = true;
|
|
item.SaveToken = true;
|
|
item.TokenValidationParameters = new TokenValidationParameters()
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authkey)),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ClockSkew = TimeSpan.Zero
|
|
};
|
|
});
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
builder.Services.AddScoped<IQuestionsProvider, QuestionsProvider>();
|
|
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
//builder.Services.AddSwaggerGen();
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
// Include XML comments from your assembly
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
options.IncludeXmlComments(xmlPath);
|
|
|
|
OpenApiSecurityScheme securityDefinition = new OpenApiSecurityScheme()
|
|
{
|
|
Name = "Bearer",
|
|
BearerFormat = "JWT",
|
|
Scheme = "bearer",
|
|
Description = "Specify the authorization token.",
|
|
In = ParameterLocation.Header,
|
|
Type = SecuritySchemeType.Http,
|
|
};
|
|
|
|
options.AddSecurityDefinition("jwt_auth", securityDefinition);
|
|
|
|
// Make sure swagger UI requires a Bearer token specified
|
|
OpenApiSecurityScheme securityScheme = new OpenApiSecurityScheme()
|
|
{
|
|
Reference = new OpenApiReference()
|
|
{
|
|
Id = "jwt_auth",
|
|
Type = ReferenceType.SecurityScheme
|
|
}
|
|
};
|
|
OpenApiSecurityRequirement securityRequirements = new OpenApiSecurityRequirement()
|
|
{
|
|
{securityScheme, new string[] { }},
|
|
};
|
|
options.AddSecurityRequirement(securityRequirements);
|
|
});
|
|
|
|
builder.Services.AddDbContext<QuestionDbContext>(option =>
|
|
{
|
|
option.UseSqlServer("QuestionConnection");
|
|
});
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
using (var serviceScope = app.Services.CreateScope())
|
|
{
|
|
var services = serviceScope.ServiceProvider;
|
|
var questionProvider = services.GetRequiredService<IQuestionsProvider>();
|
|
questionProvider.SeedData();
|
|
}
|
|
}
|
|
app.UseCors("DamageAppCorsPolicy");
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|