-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
79 lines (69 loc) · 2.46 KB
/
Program.cs
File metadata and controls
79 lines (69 loc) · 2.46 KB
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
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.IdentityModel.Tokens;
using SocialAPI;
using SocialAPI.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<NoteDbContext>();
// TODO: Replace with asymmetric key
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(jwtOptions =>
{
jwtOptions.Authority = "C#-Notes-App";
// Only required for development - should be removed in prod
// FIXME
jwtOptions.RequireHttpsMetadata = false;
jwtOptions.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = JwtConst.Issuer,
ValidAudience = JwtConst.Audience,
IssuerSigningKey = JwtConst.PublicKey,
// IssuerSigningKey = new SymmetricSecurityKey(
// new byte[256]
// )
};
});
builder.Services.AddAuthorization();
// builder.Services.AddDbContext<NoteDBContext>(options => {});
// builder.Services.AddDbContextPool<NotesContext>(opt =>
// opt.UseNpgsql(builder.Configuration.GetConnectionString("NotesContext")));
// builder.Services.AddNpgsqlDataSource(
// "Server=localhost;Port=5432;Database=c_sharp_notes;User ID=c_sharp_notes;Password=cSharp;"
// );
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Call EnsureCreated() to create the database and tables
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var dbContext = services.GetRequiredService<NoteDbContext>();
dbContext.Database.EnsureCreated(); // Creates database/tables if missing
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred creating the database.");
}
}
app.Run();