forked from markjprice/web-dev-net9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
112 lines (89 loc) · 3.16 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Microsoft.AspNetCore.Mvc.Formatters; // To use IOutputFormatter.
using Northwind.EntityModels; // To use AddNorthwindContext method.
using Northwind.Repositories; // To use ICustomerRepository.
using Microsoft.Extensions.Caching.Hybrid; // To use HybridCacheEntryOptions.
using Microsoft.AspNetCore.HttpLogging; // To use HttpLoggingFields.
using System.Security.Claims; // To use ClaimsPrincipal.
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(defaultScheme: "Bearer")
.AddJwtBearer();
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "Northwind.Mvc.Policy",
policy =>
{
policy.WithOrigins("https://localhost:5021");
});
});
builder.Services.AddNorthwindContext();
builder.Services.AddControllers(options =>
{
WriteLine("Default output formatters:");
foreach (IOutputFormatter formatter in options.OutputFormatters)
{
OutputFormatter? mediaFormatter = formatter as OutputFormatter;
if (mediaFormatter is null)
{
WriteLine($" {formatter.GetType().Name}");
}
else // OutputFormatter class has SupportedMediaTypes.
{
WriteLine(" {0}, Media types: {1}",
arg0: mediaFormatter.GetType().Name,
arg1: string.Join(", ",
mediaFormatter.SupportedMediaTypes));
}
}
})
.AddXmlDataContractSerializerFormatters()
.AddXmlSerializerFormatters();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
builder.Services.AddHybridCache(options =>
{
options.DefaultEntryOptions = new HybridCacheEntryOptions
{
Expiration = TimeSpan.FromSeconds(60),
LocalCacheExpiration = TimeSpan.FromSeconds(30)
};
});
builder.Services.AddResponseCaching();
builder.Services.AddHttpLogging(options =>
{
// Add the Origin header so it will not be redacted.
options.RequestHeaders.Add("Origin");
options.LoggingFields = HttpLoggingFields.All;
options.RequestBodyLogLimit = 4096; // Default is 32k.
options.ResponseBodyLogLimit = 4096; // Default is 32k.
});
var app = builder.Build();
app.UseCors(policyName: "Northwind.Mvc.Policy");
app.UseHttpLogging();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseResponseCaching();
app.UseAuthorization();
app.MapControllers();
app.MapGet("/secret", (ClaimsPrincipal user) =>
string.Format("Welcome, {0}. The secret ingredient is love.",
user.Identity?.Name ?? "secure user"))
.RequireAuthorization();
string? tunnelUrl = Environment.GetEnvironmentVariable("VS_TUNNEL_URL");
if (tunnelUrl is not null)
{
WriteLine($"Tunnel URL: {tunnelUrl}");
}
app.Run();
}
}