-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
83 lines (69 loc) · 2.92 KB
/
Copy pathProgram.cs
File metadata and controls
83 lines (69 loc) · 2.92 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using CommerceApi.Services;
using CommerceApi.Data;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(opt =>
opt.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
var jwtKey = builder.Configuration["Jwt:Key"]!;
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => {
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)),
NameClaimType = System.Security.Claims.ClaimTypes.Name,
RoleClaimType = System.Security.Claims.ClaimTypes.Role
};
options.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
var claims = context.Principal?.Claims.Select(c => $"{c.Type}: {c.Value}");
System.Diagnostics.Debug.WriteLine("Claims: " + string.Join(", ", claims ?? Array.Empty<string>()));
return Task.CompletedTask;
},
OnAuthenticationFailed = context =>
{
System.Diagnostics.Debug.WriteLine("ERROR");
return Task.CompletedTask;
},
OnChallenge = context =>
{
if (context.AuthenticateFailure == null && string.IsNullOrEmpty(context.Error))
{
System.Diagnostics.Debug.WriteLine("MISSING");
}
return Task.CompletedTask;
}
};
});
builder.Services.AddMemoryCache();
builder.Services.AddScoped<IAuthService, AuthService>();
// Product Service with Cache Decorator
builder.Services.AddScoped<ProductService>();
builder.Services.AddScoped<IProductService, CachedProductService>();
// Category Service with Cache Decorator
builder.Services.AddScoped<CategoryService>();
builder.Services.AddScoped<ICategoryService, CachedCategoryService>();
builder.Services.AddScoped<ICartService, CartService>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<IPaymentService, PaymentService>();
builder.Services.AddScoped<IFileService, FileService>();
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();