-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
59 lines (44 loc) · 1.65 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
using System.Net;
using System.Net.Security;
/*
* Add handler code to ServicePointManager
* This works for older versions of .NET (before 2.1) and for older APIs (e.g. WebRequest)
* This DOES NOT work for HttpClient on newer .NET Core
*
* 7890C8934D5869B25D2F8D0D646F9A5D7385BA85 is the thumbprint for the root authority of https://untrusted-root.badssl.com/
*/
ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) =>
{
if (error == SslPolicyErrors.None)
return true;
if (error == SslPolicyErrors.RemoteCertificateChainErrors)
return chain.ChainElements[^1].Certificate.Thumbprint == "7890C8934D5869B25D2F8D0D646F9A5D7385BA85".ToUpper();
return false;
};
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// add HttpClient as a singleton (best practice?) dependency injection
// any page / service using HttpClient can just "request" it.
builder.Services.AddSingleton<HttpClient>(new HttpClient(new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (msg, cert, chain, error) =>
{
if (error == SslPolicyErrors.None)
return true;
if (error == SslPolicyErrors.RemoteCertificateChainErrors)
return chain.ChainElements[^1].Certificate.Thumbprint == "7890C8934D5869B25D2F8D0D646F9A5D7385BA85".ToUpper();
return false;
}
}));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();