-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
97 lines (90 loc) · 3.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//--------------------------------------------------------------------------------------------------------------------------
//CRUD with Authorization by Policy with search & filter & pagination metadata. Documentation completed for sample methods.|
//--------------------------------------------------------------------------------------------------------------------------
///Logger
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/productsinfo.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddControllers().AddNewtonsoftJson();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(setup =>
{
var xmlCommentsFile = $"comments.xml";
var xmlCommentsFullPath = Path.Combine(AppContext.BaseDirectory,xmlCommentsFile);
setup.IncludeXmlComments(xmlCommentsFullPath);
setup.AddSecurityDefinition("ProductAPI-BearerAuth", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
Description = "Input a valid token to access this API."
});
setup.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "ProductAPI-BearerAuth" }
}, new List<string>() }
});
});
#if DEBUG
builder.Services.AddTransient<IMailService,MailService>();
#else
builder.Services.AddTransient<IMailService, CloudMailService>();
#endif
builder.Services.AddDbContext<ProductDb>(dbContextOptions => dbContextOptions.
UseSqlite(builder.Configuration["ConnectionStrings:ProductDbCS"]));
builder.Services.AddScoped<IProductRepo, ProductRepo>();
builder.Host.UseSerilog();
builder.Services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Authentication:Issuer"],
ValidAudience = builder.Configuration["Authentication:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.ASCII.GetBytes(builder.Configuration["Authentication:SecretForKey"]))
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("TestPolicy", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("mail", "[email protected]");
});
});
builder.Services.AddApiVersioning(versionConfiguration =>
{
versionConfiguration.AssumeDefaultVersionWhenUnspecified = true;
versionConfiguration.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
versionConfiguration.ReportApiVersions = true;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();