Skip to content

Commit 6097145

Browse files
authored
[Security] Move to GenericHost (#24282)
1 parent 780d527 commit 6097145

File tree

12 files changed

+335
-211
lines changed

12 files changed

+335
-211
lines changed
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
using Microsoft.AspNetCore;
1+
using System.Threading.Tasks;
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.AspNetCore.Server.Kestrel.Https;
4+
using Microsoft.Extensions.Hosting;
45

56
namespace Certificate.Sample
67
{
78
public class Program
89
{
9-
public static void Main(string[] args)
10+
public static Task Main(string[] args)
1011
{
11-
BuildWebHost(args).Run();
12-
}
13-
14-
public static IWebHost BuildWebHost(string[] args)
15-
=> WebHost.CreateDefaultBuilder(args)
16-
.UseStartup<Startup>()
17-
.ConfigureKestrel(options =>
18-
{
19-
options.ConfigureHttpsDefaults(opt =>
12+
var host = Host.CreateDefaultBuilder(args)
13+
.ConfigureWebHost(webHostBuilder =>
2014
{
21-
opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
22-
});
23-
})
24-
.Build();
15+
webHostBuilder
16+
.UseStartup<Startup>()
17+
.ConfigureKestrel(options =>
18+
{
19+
options.ConfigureHttpsDefaults(opt =>
20+
{
21+
opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
22+
});
23+
});
24+
})
25+
.Build();
26+
27+
return host.RunAsync();
28+
}
2529
}
2630
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
using System.IO;
1+
using System.IO;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Hosting;
35
using Microsoft.Extensions.Logging;
46

57
namespace CookieSample
68
{
79
public static class Program
810
{
9-
public static void Main(string[] args)
11+
public static Task Main(string[] args)
1012
{
11-
var host = new WebHostBuilder()
13+
var host = new HostBuilder()
14+
.ConfigureWebHost(webHostBuilder =>
15+
{
16+
webHostBuilder
17+
.UseKestrel()
18+
.UseContentRoot(Directory.GetCurrentDirectory())
19+
.UseIISIntegration()
20+
.UseStartup<Startup>();
21+
})
1222
.ConfigureLogging(factory =>
1323
{
1424
factory.AddConsole();
1525
factory.AddFilter("Console", level => level >= LogLevel.Information);
1626
})
17-
.UseKestrel()
18-
.UseContentRoot(Directory.GetCurrentDirectory())
19-
.UseIISIntegration()
20-
.UseStartup<Startup>()
2127
.Build();
2228

23-
host.Run();
29+
return host.RunAsync();
2430
}
2531
}
2632
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
using System.IO;
1+
using System.IO;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Hosting;
35
using Microsoft.Extensions.Logging;
46

57
namespace CookieSessionSample
68
{
79
public static class Program
810
{
9-
public static void Main(string[] args)
11+
public static Task Main(string[] args)
1012
{
11-
var host = new WebHostBuilder()
13+
var host = new HostBuilder()
14+
.ConfigureWebHost(webHostBuilder =>
15+
{
16+
webHostBuilder
17+
.UseKestrel()
18+
.UseContentRoot(Directory.GetCurrentDirectory())
19+
.UseIISIntegration()
20+
.UseStartup<Startup>();
21+
})
1222
.ConfigureLogging(factory =>
1323
{
1424
factory.AddConsole();
1525
factory.AddFilter("Console", level => level >= LogLevel.Information);
1626
})
17-
.UseKestrel()
18-
.UseContentRoot(Directory.GetCurrentDirectory())
19-
.UseIISIntegration()
20-
.UseStartup<Startup>()
2127
.Build();
2228

23-
host.Run();
29+
return host.RunAsync();
2430
}
2531
}
2632
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
using Microsoft.AspNetCore;
1+
using System.Threading.Tasks;
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.Extensions.Hosting;
44

55
namespace JwtBearerSample
66
{
77
public static class Program
88
{
9-
public static void Main(string[] args)
9+
public static Task Main(string[] args)
1010
{
11-
var host = WebHost.CreateDefaultBuilder(args)
12-
.UseStartup<Startup>()
11+
var host = Host.CreateDefaultBuilder(args)
12+
.ConfigureWebHostDefaults(webHostBuilder =>
13+
{
14+
webHostBuilder
15+
.UseStartup<Startup>();
16+
})
1317
.Build();
1418

15-
host.Run();
19+
return host.RunAsync();
1620
}
1721
}
1822
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
using Microsoft.AspNetCore;
1+
using System.Threading.Tasks;
22
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Hosting;
34

45
namespace OpenIdConnect.AzureAdSample
56
{
67
public static class Program
78
{
8-
public static void Main(string[] args)
9+
public static Task Main(string[] args)
910
{
10-
var host = WebHost.CreateDefaultBuilder(args)
11-
.UseStartup<Startup>()
11+
var host = Host.CreateDefaultBuilder(args)
12+
.ConfigureWebHostDefaults(webHostBuilder =>
13+
{
14+
webHostBuilder
15+
.UseStartup<Startup>();
16+
})
1217
.Build();
1318

14-
host.Run();
19+
return host.RunAsync();
1520
}
1621
}
1722
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11

2-
using Microsoft.AspNetCore;
2+
using System.Threading.Tasks;
33
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Hosting;
45

56
namespace OpenIdConnectSample
67
{
78
public static class Program
89
{
9-
public static void Main(string[] args)
10+
public static Task Main(string[] args)
1011
{
11-
var host = WebHost.CreateDefaultBuilder(args)
12-
.UseStartup<Startup>()
12+
var host = Host.CreateDefaultBuilder(args)
13+
.ConfigureWebHostDefaults(webHostBuilder =>
14+
{
15+
webHostBuilder
16+
.UseStartup<Startup>();
17+
})
1318
.Build();
1419

15-
host.Run();
20+
return host.RunAsync();
1621
}
1722
}
1823
}

src/Security/Authentication/WsFederation/samples/WsFedSample/Program.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.IO;
4-
using System.Linq;
52
using System.Net;
63
using System.Reflection;
74
using System.Security.Cryptography.X509Certificates;
85
using System.Threading.Tasks;
9-
using Microsoft.AspNetCore;
106
using Microsoft.AspNetCore.Hosting;
11-
using Microsoft.Extensions.Configuration;
127
using Microsoft.Extensions.FileProviders;
8+
using Microsoft.Extensions.Hosting;
139
using Microsoft.Extensions.Logging;
1410

1511
namespace WsFedSample
1612
{
1713
public class Program
1814
{
19-
public static void Main(string[] args)
15+
public static Task Main(string[] args)
2016
{
21-
var host = new WebHostBuilder()
17+
var host = new HostBuilder()
18+
.ConfigureWebHost(webHostBuilder =>
19+
{
20+
webHostBuilder
21+
.UseKestrel(options =>
22+
{
23+
options.Listen(IPAddress.Loopback, 44307, listenOptions =>
24+
{
25+
// Configure SSL
26+
var serverCertificate = LoadCertificate();
27+
listenOptions.UseHttps(serverCertificate);
28+
});
29+
})
30+
.UseContentRoot(Directory.GetCurrentDirectory())
31+
.UseIISIntegration()
32+
.UseStartup<Startup>();
33+
})
2234
.ConfigureLogging(factory =>
2335
{
2436
factory.AddConsole();
2537
factory.AddDebug();
2638
factory.AddFilter("Console", level => level >= LogLevel.Information);
2739
factory.AddFilter("Debug", level => level >= LogLevel.Information);
2840
})
29-
.UseKestrel(options =>
30-
{
31-
options.Listen(IPAddress.Loopback, 44307, listenOptions =>
32-
{
33-
// Configure SSL
34-
var serverCertificate = LoadCertificate();
35-
listenOptions.UseHttps(serverCertificate);
36-
});
37-
})
38-
.UseContentRoot(Directory.GetCurrentDirectory())
39-
.UseIISIntegration()
40-
.UseStartup<Startup>()
4141
.Build();
4242

43-
host.Run();
43+
return host.RunAsync();
4444
}
4545

4646
private static X509Certificate2 LoadCertificate()

src/Security/Authentication/test/OpenIdConnect/OpenIdConnectConfigurationTests.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,22 @@ public async Task SpecificForwardWinsOverSelectorAndDefault()
437437
[Fact]
438438
public async Task MetadataAddressIsGeneratedFromAuthorityWhenMissing()
439439
{
440-
var builder = new WebHostBuilder()
440+
using var host = new HostBuilder()
441+
.ConfigureWebHost(webHostBuilder =>
442+
{
443+
webHostBuilder
444+
.Configure(app =>
445+
{
446+
app.UseAuthentication();
447+
app.Run(async context =>
448+
{
449+
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
450+
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
451+
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
452+
});
453+
})
454+
.UseTestServer();
455+
})
441456
.ConfigureServices(services =>
442457
{
443458
services.AddAuthentication()
@@ -449,17 +464,11 @@ public async Task MetadataAddressIsGeneratedFromAuthorityWhenMissing()
449464
o.SignInScheme = Guid.NewGuid().ToString();
450465
});
451466
})
452-
.Configure(app =>
453-
{
454-
app.UseAuthentication();
455-
app.Run(async context =>
456-
{
457-
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
458-
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
459-
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
460-
});
461-
});
462-
var server = new TestServer(builder);
467+
.Build();
468+
469+
var server = host.GetTestServer();
470+
471+
await host.StartAsync();
463472
var transaction = await server.SendAsync(@"https://example.com");
464473
Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode);
465474
}

0 commit comments

Comments
 (0)