[Security] Move to GenericHost (#24282)

This commit is contained in:
Kahbazi 2020-07-28 00:39:15 +04:30 committed by GitHub
parent 780d527f0b
commit 6097145096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 335 additions and 211 deletions

View File

@ -1,18 +1,18 @@
using Microsoft.AspNetCore; using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.Extensions.Hosting;
namespace Certificate.Sample namespace Certificate.Sample
{ {
public class Program public class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
BuildWebHost(args).Run(); var host = Host.CreateDefaultBuilder(args)
} .ConfigureWebHost(webHostBuilder =>
{
public static IWebHost BuildWebHost(string[] args) webHostBuilder
=> WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>() .UseStartup<Startup>()
.ConfigureKestrel(options => .ConfigureKestrel(options =>
{ {
@ -20,7 +20,11 @@ namespace Certificate.Sample
{ {
opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate; opt.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
}); });
});
}) })
.Build(); .Build();
return host.RunAsync();
}
} }
} }

View File

@ -1,26 +1,32 @@
using System.IO; using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CookieSample namespace CookieSample
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.ConfigureLogging(factory => .ConfigureLogging(factory =>
{ {
factory.AddConsole(); factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Information); factory.AddFilter("Console", level => level >= LogLevel.Information);
}) })
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -1,26 +1,32 @@
using System.IO; using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CookieSessionSample namespace CookieSessionSample
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.ConfigureLogging(factory => .ConfigureLogging(factory =>
{ {
factory.AddConsole(); factory.AddConsole();
factory.AddFilter("Console", level => level >= LogLevel.Information); factory.AddFilter("Console", level => level >= LogLevel.Information);
}) })
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -1,4 +1,4 @@
using Microsoft.AspNetCore; using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -6,13 +6,17 @@ namespace JwtBearerSample
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = WebHost.CreateDefaultBuilder(args) var host = Host.CreateDefaultBuilder(args)
.UseStartup<Startup>() .ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -1,17 +1,22 @@
using Microsoft.AspNetCore; using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace OpenIdConnect.AzureAdSample namespace OpenIdConnect.AzureAdSample
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = WebHost.CreateDefaultBuilder(args) var host = Host.CreateDefaultBuilder(args)
.UseStartup<Startup>() .ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -1,18 +1,23 @@
using Microsoft.AspNetCore; using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace OpenIdConnectSample namespace OpenIdConnectSample
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = WebHost.CreateDefaultBuilder(args) var host = Host.CreateDefaultBuilder(args)
.UseStartup<Startup>() .ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder
.UseStartup<Startup>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -1,31 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace WsFedSample namespace WsFedSample
{ {
public class Program public class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.ConfigureLogging(factory => .ConfigureWebHost(webHostBuilder =>
{ {
factory.AddConsole(); webHostBuilder
factory.AddDebug();
factory.AddFilter("Console", level => level >= LogLevel.Information);
factory.AddFilter("Debug", level => level >= LogLevel.Information);
})
.UseKestrel(options => .UseKestrel(options =>
{ {
options.Listen(IPAddress.Loopback, 44307, listenOptions => options.Listen(IPAddress.Loopback, 44307, listenOptions =>
@ -37,10 +29,18 @@ namespace WsFedSample
}) })
.UseContentRoot(Directory.GetCurrentDirectory()) .UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration() .UseIISIntegration()
.UseStartup<Startup>() .UseStartup<Startup>();
})
.ConfigureLogging(factory =>
{
factory.AddConsole();
factory.AddDebug();
factory.AddFilter("Console", level => level >= LogLevel.Information);
factory.AddFilter("Debug", level => level >= LogLevel.Information);
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
private static X509Certificate2 LoadCertificate() private static X509Certificate2 LoadCertificate()

View File

@ -437,7 +437,22 @@ namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect
[Fact] [Fact]
public async Task MetadataAddressIsGeneratedFromAuthorityWhenMissing() public async Task MetadataAddressIsGeneratedFromAuthorityWhenMissing()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app =>
{
app.UseAuthentication();
app.Run(async context =>
{
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
});
})
.UseTestServer();
})
.ConfigureServices(services => .ConfigureServices(services =>
{ {
services.AddAuthentication() services.AddAuthentication()
@ -449,17 +464,11 @@ namespace Microsoft.AspNetCore.Authentication.Test.OpenIdConnect
o.SignInScheme = Guid.NewGuid().ToString(); o.SignInScheme = Guid.NewGuid().ToString();
}); });
}) })
.Configure(app => .Build();
{
app.UseAuthentication(); var server = host.GetTestServer();
app.Run(async context =>
{ await host.StartAsync();
var resolver = context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>();
var handler = await resolver.GetHandlerAsync(context, OpenIdConnectDefaults.AuthenticationScheme) as OpenIdConnectHandler;
Assert.Equal($"{TestServerBuilder.DefaultAuthority}/.well-known/openid-configuration", handler.Options.MetadataAddress);
});
});
var server = new TestServer(builder);
var transaction = await server.SendAsync(@"https://example.com"); var transaction = await server.SendAsync(@"https://example.com");
Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode); Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode);
} }

View File

@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Authentication namespace Microsoft.AspNetCore.Authentication
@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Authentication
[Fact] [Fact]
public async Task CanDispatch() public async Task CanDispatch()
{ {
var server = CreateServer(services => using var server = await CreateServer(services =>
{ {
services.AddLogging().AddAuthentication(o => services.AddLogging().AddAuthentication(o =>
{ {
@ -333,7 +334,7 @@ namespace Microsoft.AspNetCore.Authentication
[Fact] [Fact]
public async Task CanDynamicTargetBasedOnQueryString() public async Task CanDynamicTargetBasedOnQueryString()
{ {
var server = CreateServer(services => using var server = await CreateServer(services =>
{ {
services.AddAuthentication(o => services.AddAuthentication(o =>
{ {
@ -455,9 +456,12 @@ namespace Microsoft.AspNetCore.Authentication
} }
} }
private static TestServer CreateServer(Action<IServiceCollection> configure = null, string defaultScheme = null) private static async Task<TestServer> CreateServer(Action<IServiceCollection> configure = null, string defaultScheme = null)
{ {
var builder = new WebHostBuilder() var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app => .Configure(app =>
{ {
app.UseAuthentication(); app.UseAuthentication();
@ -477,11 +481,19 @@ namespace Microsoft.AspNetCore.Authentication
} }
}); });
}) })
.UseTestServer();
})
.ConfigureServices(services => .ConfigureServices(services =>
{ {
configure?.Invoke(services); configure?.Invoke(services);
}); })
return new TestServer(builder); .Build();
var server = host.GetTestServer();
await host.StartAsync();
return server;
} }
} }
} }

View File

@ -1,26 +1,32 @@
using System.IO; using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace CookiePolicySample namespace CookiePolicySample
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
})
.ConfigureLogging(factory => .ConfigureLogging(factory =>
{ {
factory.AddConsole(); factory.AddConsole();
factory.AddFilter("Microsoft", LogLevel.Trace); factory.AddFilter("Microsoft", LogLevel.Trace);
}) })
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using Xunit; using Xunit;
@ -641,20 +642,30 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
Assert.NotNull(manualCookie.Expires); // Expires may not exactly match to the second. Assert.NotNull(manualCookie.Expires); // Expires may not exactly match to the second.
} }
private Task<HttpContext> RunTestAsync(Action<CookiePolicyOptions> configureOptions, Action<HttpContext> configureRequest, RequestDelegate handleRequest) private async Task<HttpContext> RunTestAsync(Action<CookiePolicyOptions> configureOptions, Action<HttpContext> configureRequest, RequestDelegate handleRequest)
{ {
var builder = new WebHostBuilder() var host = new HostBuilder()
.ConfigureServices(services => .ConfigureWebHost(webHostBuilder =>
{ {
services.Configure(configureOptions); webHostBuilder
})
.Configure(app => .Configure(app =>
{ {
app.UseCookiePolicy(); app.UseCookiePolicy();
app.Run(handleRequest); app.Run(handleRequest);
}); })
var server = new TestServer(builder); .UseTestServer();
return server.SendAsync(configureRequest); })
.ConfigureServices(services =>
{
services.Configure(configureOptions);
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
return await server.SendAsync(configureRequest);
} }
} }
} }

View File

@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using Xunit; using Xunit;
@ -244,7 +245,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact] [Fact]
public async Task CookiePolicyCanHijackAppend() public async Task CookiePolicyCanHijackAppend()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app => .Configure(app =>
{ {
app.UseCookiePolicy(new CookiePolicyOptions app.UseCookiePolicy(new CookiePolicyOptions
@ -259,8 +263,14 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
context.Response.Cookies.Append("D", "D", new CookieOptions { Secure = true }); context.Response.Cookies.Append("D", "D", new CookieOptions { Secure = true });
return Task.FromResult(0); return Task.FromResult(0);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login"); var transaction = await server.SendAsync("http://example.com/login");
@ -274,7 +284,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact] [Fact]
public async Task CookiePolicyCanHijackDelete() public async Task CookiePolicyCanHijackDelete()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app => .Configure(app =>
{ {
app.UseCookiePolicy(new CookiePolicyOptions app.UseCookiePolicy(new CookiePolicyOptions
@ -289,8 +302,14 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
context.Response.Cookies.Delete("D", new CookieOptions { Secure = true }); context.Response.Cookies.Delete("D", new CookieOptions { Secure = true });
return Task.FromResult(0); return Task.FromResult(0);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login"); var transaction = await server.SendAsync("http://example.com/login");
@ -302,7 +321,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact] [Fact]
public async Task CookiePolicyCallsCookieFeature() public async Task CookiePolicyCallsCookieFeature()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app => .Configure(app =>
{ {
app.Use(next => context => app.Use(next => context =>
@ -322,8 +344,14 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
Assert.Throws<NotImplementedException>(() => context.Response.Cookies.Append("A", "A", new CookieOptions())); Assert.Throws<NotImplementedException>(() => context.Response.Cookies.Append("A", "A", new CookieOptions()));
return context.Response.WriteAsync("Done"); return context.Response.WriteAsync("Done");
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login"); var transaction = await server.SendAsync("http://example.com/login");
Assert.Equal("Done", transaction.ResponseText); Assert.Equal("Done", transaction.ResponseText);
@ -332,16 +360,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact] [Fact]
public async Task CookiePolicyAppliesToCookieAuth() public async Task CookiePolicyAppliesToCookieAuth()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureServices(services => .ConfigureWebHost(webHostBuilder =>
{ {
services.AddAuthentication().AddCookie(o => webHostBuilder
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
});
})
.Configure(app => .Configure(app =>
{ {
app.UseCookiePolicy(new CookiePolicyOptions app.UseCookiePolicy(new CookiePolicyOptions
@ -355,8 +377,23 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
return context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, return context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("TestUser", "Cookies")))); new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("TestUser", "Cookies"))));
}); });
})
.UseTestServer();
})
.ConfigureServices(services =>
{
services.AddAuthentication().AddCookie(o =>
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
}); });
var server = new TestServer(builder); })
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login"); var transaction = await server.SendAsync("http://example.com/login");
@ -372,16 +409,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
[Fact] [Fact]
public async Task CookiePolicyAppliesToCookieAuthChunks() public async Task CookiePolicyAppliesToCookieAuthChunks()
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureServices(services => .ConfigureWebHost(webHostBuilder =>
{ {
services.AddAuthentication().AddCookie(o => webHostBuilder
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
});
})
.Configure(app => .Configure(app =>
{ {
app.UseCookiePolicy(new CookiePolicyOptions app.UseCookiePolicy(new CookiePolicyOptions
@ -395,8 +426,23 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
return context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, return context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity(new string('c', 1024 * 5), "Cookies")))); new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity(new string('c', 1024 * 5), "Cookies"))));
}); });
})
.UseTestServer();
})
.ConfigureServices(services =>
{
services.AddAuthentication().AddCookie(o =>
{
o.Cookie.Name = "TestCookie";
o.Cookie.HttpOnly = false;
o.Cookie.SecurePolicy = CookieSecurePolicy.None;
}); });
var server = new TestServer(builder); })
.Build();
var server = host.GetTestServer();
await host.StartAsync();
var transaction = await server.SendAsync("http://example.com/login"); var transaction = await server.SendAsync("http://example.com/login");
@ -475,7 +521,10 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
RequestDelegate configureSetup, RequestDelegate configureSetup,
params RequestTest[] tests) params RequestTest[] tests)
{ {
var builder = new WebHostBuilder() using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.Configure(app => .Configure(app =>
{ {
app.Map(path, map => app.Map(path, map =>
@ -483,8 +532,15 @@ namespace Microsoft.AspNetCore.CookiePolicy.Test
map.UseCookiePolicy(cookiePolicy); map.UseCookiePolicy(cookiePolicy);
map.Run(configureSetup); map.Run(configureSetup);
}); });
}); })
var server = new TestServer(builder); .UseTestServer();
})
.Build();
var server = host.GetTestServer();
await host.StartAsync();
foreach (var test in tests) foreach (var test in tests)
{ {
await test.Execute(server); await test.Execute(server);