-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
87 lines (73 loc) · 2.81 KB
/
Program.cs
File metadata and controls
87 lines (73 loc) · 2.81 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
80
81
82
83
84
85
86
87
using AuthService.API.Services;
using AuthService.API.Interfaces;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
//to register DB context if using a real database
using Microsoft.EntityFrameworkCore;
using AuthService.API.Data; //the folder where dbcontext is in
using AuthService.API.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
//jwt authentication configuration:
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtSettings:Issuer"],
ValidAudience = builder.Configuration["JwtSettings:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Key"]))
};
});
//add authorization services - this enables autherization so we can protect endpoints with [Authorize] attribute
builder.Services.AddAuthorization();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//Register AuthService for dependency injection
//builder.Services.AddSingleton <IAuthService,AuthenticationService>(); (while using in memory list)
builder.Services.AddScoped<IAuthService, AuthenticationService>(); //when using a real database, use scoped lifetime
//register db context for dependency injection
builder.Services.AddDbContext<AuthDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
//admin seeding logic
using(var scope=app.Services.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<AuthDbContext>();
if(!context.Users.Any(u=>u.Role=="Admin"))
{
var admin = new User
{
Username = "admin",
Email = "admin@ecom.com",
PasswordHash = BCrypt.Net.BCrypt.HashPassword("Admin@123"), //hash the password
Role = "Admin",
CreatedAt = DateTime.UtcNow,
IsActive = true
};
context.Users.Add(admin);
context.SaveChanges();
}
}
//enable authentication in pipeline - order matters: authentication first, then authorization
app.UseAuthentication();
app.UseAuthorization();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();