DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/Program.cs

98 lines
3.2 KiB
C#
Raw Normal View History

2023-09-22 10:52:17 -05:00
using DamageAssesment.Api.DocuLinks.Db;
using DamageAssesment.Api.DocuLinks.Interfaces;
using DamageAssesment.Api.DocuLinks.Providers;
2023-08-31 18:00:51 -05:00
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
2023-09-13 15:49:59 -05:00
using System.Reflection;
using System.Text;
using Microsoft.OpenApi.Models;
2023-08-31 18:00:51 -05:00
var builder = WebApplication.CreateBuilder(args);
2023-11-25 23:08:04 -05:00
builder.Services.AddCors(p => p.AddPolicy("DamageAppCorsPolicy", build => {
build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
}));
2023-08-31 18:00:51 -05:00
// Add services to the container.
var authkey = builder.Configuration.GetValue<string>("JwtSettings:securitykey");
2023-11-25 23:08:04 -05:00
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
};
});
2023-08-31 18:00:51 -05:00
builder.Services.AddControllers();
builder.Services.AddSwaggerGen(options =>
2023-09-13 15:49:59 -05:00
{
// 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);
2023-09-13 15:49:59 -05:00
});
2023-08-31 18:00:51 -05:00
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
2023-09-22 10:52:17 -05:00
builder.Services.AddScoped<IDoculinkProvider, DoculinkProvider>();
2023-08-31 18:00:51 -05:00
builder.Services.AddScoped<IUploadService, UploadService>();
builder.Services.AddScoped<IAzureBlobService, AzureBlobService>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30
2023-09-22 10:52:17 -05:00
builder.Services.AddDbContext<DoculinkDbContext>(option =>
2023-08-31 18:00:51 -05:00
{
option.UseSqlServer("DoculinkConnection");
2023-08-31 18:00:51 -05:00
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
2023-11-25 23:08:04 -05:00
app.UseCors("DamageAppCorsPolicy");
app.UseAuthentication();
2023-08-31 18:00:51 -05:00
app.UseAuthorization();
app.MapControllers();
app.Run();