diff --git a/samples/CookieSample/Properties/launchSettings.json b/samples/CookieSample/Properties/launchSettings.json index 9077103681..c85de8d26e 100644 --- a/samples/CookieSample/Properties/launchSettings.json +++ b/samples/CookieSample/Properties/launchSettings.json @@ -22,11 +22,6 @@ "environmentVariables": { "Hosting:Environment": "Development" } - }, - "kestrel": { - "commandName": "kestrel", - "launchBrowser": true, - "launchUrl": "http://localhost:5004" } } } \ No newline at end of file diff --git a/samples/CookieSample/Startup.cs b/samples/CookieSample/Startup.cs index 2d77ae1bb4..dca9105128 100644 --- a/samples/CookieSample/Startup.cs +++ b/samples/CookieSample/Startup.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Security.Claims; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -40,5 +41,15 @@ namespace CookieSample await context.Response.WriteAsync("Hello old timer"); }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } \ No newline at end of file diff --git a/samples/CookieSample/hosting.json b/samples/CookieSample/hosting.json new file mode 100644 index 0000000000..f8ef14574d --- /dev/null +++ b/samples/CookieSample/hosting.json @@ -0,0 +1,3 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel" +} diff --git a/samples/CookieSample/project.json b/samples/CookieSample/project.json index 030c149834..20c5e0249d 100644 --- a/samples/CookieSample/project.json +++ b/samples/CookieSample/project.json @@ -4,12 +4,13 @@ "Microsoft.AspNet.DataProtection": "1.0.0-*", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*" }, + "compilationOptions": { + "emitEntryPoint": true + }, "commands": { - "web": "Microsoft.AspNet.Server.Kestrel", - "weblistener": "Microsoft.AspNet.Server.WebListener" + "web": "CookieSample" }, "frameworks": { "dnx451": { }, diff --git a/samples/CookieSessionSample/Properties/launchSettings.json b/samples/CookieSessionSample/Properties/launchSettings.json index f4fbdd3fde..8d4a0316ab 100644 --- a/samples/CookieSessionSample/Properties/launchSettings.json +++ b/samples/CookieSessionSample/Properties/launchSettings.json @@ -22,11 +22,6 @@ "environmentVariables": { "Hosting:Environment": "Development" } - }, - "kestrel": { - "commandName": "kestrel", - "launchBrowser": true, - "launchUrl": "http://localhost:5004" } } } \ No newline at end of file diff --git a/samples/CookieSessionSample/Startup.cs b/samples/CookieSessionSample/Startup.cs index 5affb5afaf..bf7200ca6a 100644 --- a/samples/CookieSessionSample/Startup.cs +++ b/samples/CookieSessionSample/Startup.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Security.Claims; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -50,5 +51,15 @@ namespace CookieSessionSample await context.Response.WriteAsync("Hello old timer"); }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } \ No newline at end of file diff --git a/samples/CookieSessionSample/hosting.json b/samples/CookieSessionSample/hosting.json new file mode 100644 index 0000000000..f8ef14574d --- /dev/null +++ b/samples/CookieSessionSample/hosting.json @@ -0,0 +1,3 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel" +} diff --git a/samples/CookieSessionSample/project.json b/samples/CookieSessionSample/project.json index a1c5ad3c6c..cddabac495 100644 --- a/samples/CookieSessionSample/project.json +++ b/samples/CookieSessionSample/project.json @@ -4,13 +4,14 @@ "Microsoft.AspNet.DataProtection": "1.0.0-*", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Microsoft.Extensions.Caching.Memory": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*" }, + "compilationOptions": { + "emitEntryPoint": true + }, "commands": { - "web": "Microsoft.AspNet.Server.Kestrel", - "weblistener": "Microsoft.AspNet.Server.WebListener" + "web": "CookieSessionSample" }, "frameworks": { "dnx451": { }, diff --git a/samples/JwtBearerSample/Startup.cs b/samples/JwtBearerSample/Startup.cs index 33e8076955..88032b8e69 100644 --- a/samples/JwtBearerSample/Startup.cs +++ b/samples/JwtBearerSample/Startup.cs @@ -112,6 +112,14 @@ namespace JwtBearerSample } // Entry point for the application. - public static void Main(string[] args) => WebApplication.Run(args); + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } diff --git a/samples/JwtBearerSample/hosting.json b/samples/JwtBearerSample/hosting.json new file mode 100644 index 0000000000..f8ef14574d --- /dev/null +++ b/samples/JwtBearerSample/hosting.json @@ -0,0 +1,3 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel" +} diff --git a/samples/JwtBearerSample/project.json b/samples/JwtBearerSample/project.json index cc2350bb97..bfb35107ef 100644 --- a/samples/JwtBearerSample/project.json +++ b/samples/JwtBearerSample/project.json @@ -11,7 +11,7 @@ "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-*" }, "commands": { - "web": "Microsoft.AspNet.Server.Kestrel" + "web": "JwtBearerSample" }, "frameworks": { "dnx451": { }, diff --git a/samples/OpenIdConnectSample/Properties/launchSettings.json b/samples/OpenIdConnectSample/Properties/launchSettings.json index 3d9d32eebe..c75dba9f49 100644 --- a/samples/OpenIdConnectSample/Properties/launchSettings.json +++ b/samples/OpenIdConnectSample/Properties/launchSettings.json @@ -15,17 +15,13 @@ "ASPNET_ENV": "Development" } }, - "kestrel": { - "commandName": "kestrel", - "launchBrowser": true, - "launchUrl": "http://localhost:42023" - }, "web": { "commandName": "web", "launchBrowser": true, "launchUrl": "http://localhost:42023", "environmentVariables": { - "Hosting:Environment": "Development" + "Hosting:Environment": "Development", + "ASPNET_server.urls": "http://localhost:42023" } } } diff --git a/samples/OpenIdConnectSample/Startup.cs b/samples/OpenIdConnectSample/Startup.cs index 3e39d36541..67a979ea91 100644 --- a/samples/OpenIdConnectSample/Startup.cs +++ b/samples/OpenIdConnectSample/Startup.cs @@ -2,6 +2,7 @@ using System.Linq; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.OpenIdConnect; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.Extensions.Configuration; @@ -63,5 +64,15 @@ namespace OpenIdConnectSample await context.Response.WriteAsync("Hello Authenticated User"); }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } diff --git a/samples/OpenIdConnectSample/hosting.json b/samples/OpenIdConnectSample/hosting.json new file mode 100644 index 0000000000..f8ef14574d --- /dev/null +++ b/samples/OpenIdConnectSample/hosting.json @@ -0,0 +1,3 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel" +} diff --git a/samples/OpenIdConnectSample/project.json b/samples/OpenIdConnectSample/project.json index 21f0633824..9a204e1425 100644 --- a/samples/OpenIdConnectSample/project.json +++ b/samples/OpenIdConnectSample/project.json @@ -4,7 +4,6 @@ "Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-*", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*" }, @@ -12,10 +11,11 @@ "dnx451": { }, "dnxcore50": { } }, + "compilationOptions": { + "emitEntryPoint": true + }, "commands": { - "web": "Microsoft.AspNet.Server.Kestrel", - "kestrel": "Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:42023", - "weblistener": "Microsoft.AspNet.Server.WebListener --server.urls http://localhost:42023" + "web": "OpenIdConnectSample" }, "userSecretsId": "aspnet5-OpenIdConnectSample-20151210110318" } diff --git a/samples/SocialSample/Properties/launchSettings.json b/samples/SocialSample/Properties/launchSettings.json index dcfe9f5144..b10006b86c 100644 --- a/samples/SocialSample/Properties/launchSettings.json +++ b/samples/SocialSample/Properties/launchSettings.json @@ -10,22 +10,17 @@ "profiles": { "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, "environmentVariables": { "ASPNET_ENV": "Development" } }, - "kestrel": { - "commandName": "kestrel", - "launchBrowser": true, - "launchUrl": "http://localhost:54540/" - }, "web": { "commandName": "web", "launchBrowser": true, "launchUrl": "http://localhost:54540/", "environmentVariables": { - "Hosting:Environment": "Development" + "Hosting:Environment": "Development", + "ASPNET_server.urls": "http://localhost:54540/" } } } diff --git a/samples/SocialSample/Startup.cs b/samples/SocialSample/Startup.cs index 5ca189aa91..59c57862ae 100644 --- a/samples/SocialSample/Startup.cs +++ b/samples/SocialSample/Startup.cs @@ -12,6 +12,7 @@ using Microsoft.AspNet.Authentication.MicrosoftAccount; using Microsoft.AspNet.Authentication.OAuth; using Microsoft.AspNet.Authentication.Twitter; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.Extensions.Configuration; @@ -160,7 +161,7 @@ namespace CookieSample SaveTokensAsClaims = true }); - // You must first create an app with live.com and add it's ID and Secret to your config.json or user-secrets. + //// You must first create an app with live.com and add it's ID and Secret to your config.json or user-secrets. app.UseMicrosoftAccountAuthentication(options => { options.DisplayName = "MicrosoftAccount - Requires project changes"; @@ -323,5 +324,15 @@ namespace CookieSample await context.Response.WriteAsync(""); }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } diff --git a/samples/SocialSample/hosting.json b/samples/SocialSample/hosting.json new file mode 100644 index 0000000000..f8ef14574d --- /dev/null +++ b/samples/SocialSample/hosting.json @@ -0,0 +1,3 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel" +} diff --git a/samples/SocialSample/project.json b/samples/SocialSample/project.json index 14c7606f15..53ade790cd 100644 --- a/samples/SocialSample/project.json +++ b/samples/SocialSample/project.json @@ -8,14 +8,14 @@ "Microsoft.AspNet.DataProtection": "1.0.0-*", "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*", "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", - "Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-*", "Microsoft.Extensions.Logging.Console": "1.0.0-*" }, + "compilationOptions": { + "emitEntryPoint": true + }, "commands": { - "web": "Microsoft.AspNet.Server.Kestrel", - "kestrel": "Microsoft.AspNet.Server.Kestrel --server.urls http://localhost:54540", - "weblistener": "Microsoft.AspNet.Server.WebListener --server.urls=http://localhost:54540" + "web": "SocialSample" }, "frameworks": { "dnx451": { }, diff --git a/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs index 8af2b88780..db27680d42 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Cookies/CookieMiddlewareTests.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using System.Xml.Linq; using Microsoft.AspNet.Builder; using Microsoft.AspNet.DataProtection; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Http.Features.Authentication; @@ -748,12 +749,14 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task MapWillNotAffectChallenge() { - var server = TestServer.Create(app => + var builder = new WebApplicationBuilder() + .Configure(app => { app.UseCookieAuthentication(options => options.LoginPath = new PathString("/page")); app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Cookies", new AuthenticationProperties() { RedirectUri = "/" }))); - }, - services => services.AddAuthentication()); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/login"); @@ -767,14 +770,17 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task ChallengeDoesNotSet401OnUnauthorized() { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(); - app.Run(async context => + var builder = new WebApplicationBuilder() + .Configure(app => { - await Assert.ThrowsAsync(() => context.Authentication.ChallengeAsync()); - }); - }, services => services.AddAuthentication()); + app.UseCookieAuthentication(); + app.Run(async context => + { + await Assert.ThrowsAsync(() => context.Authentication.ChallengeAsync()); + }); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com"); Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode); @@ -783,12 +789,15 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task UseCookieWithInstanceDoesntUseSharedOptions() { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.CookieName = "One"); - app.UseCookieAuthentication(new CookieAuthenticationOptions()); - app.Run(context => context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity()))); - }, services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(options => options.CookieName = "One"); + app.UseCookieAuthentication(new CookieAuthenticationOptions()); + app.Run(context => context.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity()))); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com"); @@ -799,13 +808,15 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task MapWithSignInOnlyRedirectToReturnUrlOnLoginPath() { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.LoginPath = new PathString("/login")); - app.Map("/notlogin", signoutApp => signoutApp.Run(context => context.Authentication.SignInAsync("Cookies", - new ClaimsPrincipal()))); - }, - services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(options => options.LoginPath = new PathString("/login")); + app.Map("/notlogin", signoutApp => signoutApp.Run(context => context.Authentication.SignInAsync("Cookies", + new ClaimsPrincipal()))); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/notlogin?ReturnUrl=%2Fpage"); Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode); @@ -815,13 +826,15 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task MapWillNotAffectSignInRedirectToReturnUrl() { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.LoginPath = new PathString("/login")); - app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.SignInAsync("Cookies", - new ClaimsPrincipal()))); - }, - services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(options => options.LoginPath = new PathString("/login")); + app.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.SignInAsync("Cookies", + new ClaimsPrincipal()))); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/login?ReturnUrl=%2Fpage"); @@ -835,12 +848,14 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task MapWithSignOutOnlyRedirectToReturnUrlOnLogoutPath() { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.LogoutPath = new PathString("/logout")); - app.Map("/notlogout", signoutApp => signoutApp.Run(context => context.Authentication.SignOutAsync("Cookies"))); - }, - services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(options => options.LogoutPath = new PathString("/logout")); + app.Map("/notlogout", signoutApp => signoutApp.Run(context => context.Authentication.SignOutAsync("Cookies"))); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/notlogout?ReturnUrl=%2Fpage"); Assert.Equal(HttpStatusCode.OK, transaction.Response.StatusCode); @@ -850,12 +865,14 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task MapWillNotAffectSignOutRedirectToReturnUrl() { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.LogoutPath = new PathString("/logout")); - app.Map("/logout", signoutApp => signoutApp.Run(context => context.Authentication.SignOutAsync("Cookies"))); - }, - services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(options => options.LogoutPath = new PathString("/logout")); + app.Map("/logout", signoutApp => signoutApp.Run(context => context.Authentication.SignOutAsync("Cookies"))); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/logout?ReturnUrl=%2Fpage"); @@ -869,12 +886,14 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task MapWillNotAffectAccessDenied() { - var server = TestServer.Create(app => + var builder = new WebApplicationBuilder() + .Configure(app => { app.UseCookieAuthentication(options => options.AccessDeniedPath = new PathString("/denied")); app.Map("/forbid", signoutApp => signoutApp.Run(context => context.Authentication.ForbidAsync("Cookies"))); - }, - services => services.AddAuthentication()); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/forbid"); Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); @@ -886,13 +905,15 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task NestedMapWillNotAffectLogin() { - var server = TestServer.Create(app => - app.Map("/base", map => - { - map.UseCookieAuthentication(options => options.LoginPath = new PathString("/page")); - map.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Cookies", new AuthenticationProperties() { RedirectUri = "/" }))); - }), - services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + app.Map("/base", map => + { + map.UseCookieAuthentication(options => options.LoginPath = new PathString("/page")); + map.Map("/login", signoutApp => signoutApp.Run(context => context.Authentication.ChallengeAsync("Cookies", new AuthenticationProperties() { RedirectUri = "/" }))); + })) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/base/login"); Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); @@ -905,13 +926,15 @@ namespace Microsoft.AspNet.Authentication.Cookies [Fact] public async Task NestedMapWillNotAffectAccessDenied() { - var server = TestServer.Create(app => - app.Map("/base", map => - { - map.UseCookieAuthentication(options => options.AccessDeniedPath = new PathString("/denied")); - map.Map("/forbid", signoutApp => signoutApp.Run(context => context.Authentication.ForbidAsync("Cookies"))); - }), - services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + app.Map("/base", map => + { + map.UseCookieAuthentication(options => options.AccessDeniedPath = new PathString("/denied")); + map.Map("/forbid", signoutApp => signoutApp.Run(context => context.Authentication.ForbidAsync("Cookies"))); + })) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/base/forbid"); Assert.Equal(HttpStatusCode.Redirect, transaction.Response.StatusCode); @@ -925,39 +948,43 @@ namespace Microsoft.AspNet.Authentication.Cookies { var dp = new NoOpDataProtector(); - var server1 = TestServer.Create(app => - { - app.UseCookieAuthentication(options => + var builder1 = new WebApplicationBuilder() + .Configure(app => { - options.TicketDataFormat = new TicketDataFormat(dp); - options.CookieName = "Cookie"; - }); - app.Use((context, next) => - context.Authentication.SignInAsync("Cookies", - new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", "Cookies"))), - new AuthenticationProperties())); - }, - services => services.AddAuthentication()); + app.UseCookieAuthentication(options => + { + options.TicketDataFormat = new TicketDataFormat(dp); + options.CookieName = "Cookie"; + }); + app.Use((context, next) => + context.Authentication.SignInAsync("Cookies", + new ClaimsPrincipal(new ClaimsIdentity(new GenericIdentity("Alice", "Cookies"))), + new AuthenticationProperties())); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server1 = new TestServer(builder1); var transaction = await SendAsync(server1, "http://example.com/stuff"); Assert.NotNull(transaction.SetCookie); - var server2 = TestServer.Create(app => - { - app.UseCookieAuthentication(options => + var builder2 = new WebApplicationBuilder() + .Configure(app => { - options.AuthenticationScheme = "Cookies"; - options.CookieName = "Cookie"; - options.TicketDataFormat = new TicketDataFormat(dp); - }); - app.Use(async (context, next) => - { - var authContext = new AuthenticateContext("Cookies"); - await context.Authentication.AuthenticateAsync(authContext); - Describe(context.Response, authContext); - }); - }, - services => services.AddAuthentication()); + app.UseCookieAuthentication(options => + { + options.AuthenticationScheme = "Cookies"; + options.CookieName = "Cookie"; + options.TicketDataFormat = new TicketDataFormat(dp); + }); + app.Use(async (context, next) => + { + var authContext = new AuthenticateContext("Cookies"); + await context.Authentication.AuthenticateAsync(authContext); + Describe(context.Response, authContext); + }); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server2 = new TestServer(builder2); var transaction2 = await SendAsync(server2, "http://example.com/stuff", transaction.CookieNameValue); Assert.Equal("Alice", FindClaimValue(transaction2, ClaimTypes.Name)); } @@ -1003,71 +1030,73 @@ namespace Microsoft.AspNet.Authentication.Cookies private static TestServer CreateServer(Action configureOptions, Func testpath = null, Uri baseAddress = null, Action claimsTransform = null) { - var server = TestServer.Create(app => - { - app.UseCookieAuthentication(configureOptions); - // app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookie2" }); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(configureOptions); + // app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookie2" }); - if (claimsTransform != null) - { - app.UseClaimsTransformation(claimsTransform); - } - app.Use(async (context, next) => - { - var req = context.Request; - var res = context.Response; - PathString remainder; - if (req.Path == new PathString("/normal")) + if (claimsTransform != null) { - res.StatusCode = 200; + app.UseClaimsTransformation(claimsTransform); } - else if (req.Path == new PathString("/protected")) + app.Use(async (context, next) => { - res.StatusCode = 401; - } - else if (req.Path == new PathString("/forbid")) // Simulate forbidden - { - await context.Authentication.ForbidAsync(CookieAuthenticationDefaults.AuthenticationScheme); - } - else if (req.Path == new PathString("/challenge")) - { - await context.Authentication.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme); - } - else if (req.Path == new PathString("/signout")) - { - await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - } - else if (req.Path == new PathString("/unauthorized")) - { - await context.Authentication.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties(), ChallengeBehavior.Unauthorized); - } - else if (req.Path == new PathString("/protected/CustomRedirect")) - { - await context.Authentication.ChallengeAsync(new AuthenticationProperties() { RedirectUri = "/CustomRedirect" }); - } - else if (req.Path == new PathString("/me")) - { - var authContext = new AuthenticateContext(CookieAuthenticationDefaults.AuthenticationScheme); - authContext.Authenticated(context.User, properties: null, description: null); - Describe(res, authContext); - } - else if (req.Path.StartsWithSegments(new PathString("/me"), out remainder)) - { - var authContext = new AuthenticateContext(remainder.Value.Substring(1)); - await context.Authentication.AuthenticateAsync(authContext); - Describe(res, authContext); - } - else if (req.Path == new PathString("/testpath") && testpath != null) - { - await testpath(context); - } - else - { - await next(); - } - }); - }, - services => services.AddAuthentication()); + var req = context.Request; + var res = context.Response; + PathString remainder; + if (req.Path == new PathString("/normal")) + { + res.StatusCode = 200; + } + else if (req.Path == new PathString("/protected")) + { + res.StatusCode = 401; + } + else if (req.Path == new PathString("/forbid")) // Simulate forbidden + { + await context.Authentication.ForbidAsync(CookieAuthenticationDefaults.AuthenticationScheme); + } + else if (req.Path == new PathString("/challenge")) + { + await context.Authentication.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme); + } + else if (req.Path == new PathString("/signout")) + { + await context.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + } + else if (req.Path == new PathString("/unauthorized")) + { + await context.Authentication.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties(), ChallengeBehavior.Unauthorized); + } + else if (req.Path == new PathString("/protected/CustomRedirect")) + { + await context.Authentication.ChallengeAsync(new AuthenticationProperties() { RedirectUri = "/CustomRedirect" }); + } + else if (req.Path == new PathString("/me")) + { + var authContext = new AuthenticateContext(CookieAuthenticationDefaults.AuthenticationScheme); + authContext.Authenticated(context.User, properties: null, description: null); + Describe(res, authContext); + } + else if (req.Path.StartsWithSegments(new PathString("/me"), out remainder)) + { + var authContext = new AuthenticateContext(remainder.Value.Substring(1)); + await context.Authentication.AuthenticateAsync(authContext); + Describe(res, authContext); + } + else if (req.Path == new PathString("/testpath") && testpath != null) + { + await testpath(context); + } + else + { + await next(); + } + }); + }) + .ConfigureServices(services => services.AddAuthentication()); + var server = new TestServer(builder); server.BaseAddress = baseAddress; return server; } diff --git a/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs index c1796c3dec..5263dadb57 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Facebook/FacebookMiddlewareTests.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; @@ -13,6 +12,7 @@ using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.OAuth; using Microsoft.AspNet.Builder; using Microsoft.AspNet.DataProtection; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.TestHost; @@ -226,21 +226,23 @@ namespace Microsoft.AspNet.Authentication.Facebook private static TestServer CreateServer(Action configure, Action configureServices, Func handler) { - return TestServer.Create(app => - { - if (configure != null) + var builder = new WebApplicationBuilder() + .Configure(app => { - configure(app); - } - app.Use(async (context, next) => - { - if (handler == null || !handler(context)) + if (configure != null) { - await next(); + configure(app); } - }); - }, - configureServices); + app.Use(async (context, next) => + { + if (handler == null || !handler(context)) + { + await next(); + } + }); + }) + .ConfigureServices(configureServices); + return new TestServer(builder); } } } diff --git a/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs index 77fc762840..11fbe67e51 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Google/GoogleMiddlewareTests.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using Microsoft.AspNet.Authentication.OAuth; using Microsoft.AspNet.Builder; using Microsoft.AspNet.DataProtection; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Http.Features.Authentication; @@ -765,74 +766,76 @@ namespace Microsoft.AspNet.Authentication.Google private static TestServer CreateServer(Action configureOptions, Func testpath = null) { - return TestServer.Create(app => - { - app.UseCookieAuthentication(options => + var builder = new WebApplicationBuilder() + .Configure(app => { - options.AuthenticationScheme = TestExtensions.CookieAuthenticationScheme; - options.AutomaticAuthenticate = true; - }); - app.UseGoogleAuthentication(configureOptions); - app.UseClaimsTransformation(p => + app.UseCookieAuthentication(options => + { + options.AuthenticationScheme = TestExtensions.CookieAuthenticationScheme; + options.AutomaticAuthenticate = true; + }); + app.UseGoogleAuthentication(configureOptions); + app.UseClaimsTransformation(p => + { + var id = new ClaimsIdentity("xform"); + id.AddClaim(new Claim("xform", "yup")); + p.AddIdentity(id); + return Task.FromResult(p); + }); + app.Use(async (context, next) => + { + var req = context.Request; + var res = context.Response; + if (req.Path == new PathString("/challenge")) + { + await context.Authentication.ChallengeAsync("Google"); + } + else if (req.Path == new PathString("/me")) + { + res.Describe(context.User); + } + else if (req.Path == new PathString("/unauthorized")) + { + // Simulate Authorization failure + var result = await context.Authentication.AuthenticateAsync("Google"); + await context.Authentication.ChallengeAsync("Google"); + } + else if (req.Path == new PathString("/unauthorizedAuto")) + { + var result = await context.Authentication.AuthenticateAsync("Google"); + await context.Authentication.ChallengeAsync(); + } + else if (req.Path == new PathString("/401")) + { + res.StatusCode = 401; + } + else if (req.Path == new PathString("/signIn")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignInAsync("Google", new ClaimsPrincipal())); + } + else if (req.Path == new PathString("/signOut")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync("Google")); + } + else if (req.Path == new PathString("/forbid")) + { + await Assert.ThrowsAsync(() => context.Authentication.ForbidAsync("Google")); + } + else if (testpath != null) + { + await testpath(context); + } + else + { + await next(); + } + }); + }) + .ConfigureServices(services => { - var id = new ClaimsIdentity("xform"); - id.AddClaim(new Claim("xform", "yup")); - p.AddIdentity(id); - return Task.FromResult(p); + services.AddAuthentication(options => options.SignInScheme = TestExtensions.CookieAuthenticationScheme); }); - app.Use(async (context, next) => - { - var req = context.Request; - var res = context.Response; - if (req.Path == new PathString("/challenge")) - { - await context.Authentication.ChallengeAsync("Google"); - } - else if (req.Path == new PathString("/me")) - { - res.Describe(context.User); - } - else if (req.Path == new PathString("/unauthorized")) - { - // Simulate Authorization failure - var result = await context.Authentication.AuthenticateAsync("Google"); - await context.Authentication.ChallengeAsync("Google"); - } - else if (req.Path == new PathString("/unauthorizedAuto")) - { - var result = await context.Authentication.AuthenticateAsync("Google"); - await context.Authentication.ChallengeAsync(); - } - else if (req.Path == new PathString("/401")) - { - res.StatusCode = 401; - } - else if (req.Path == new PathString("/signIn")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignInAsync("Google", new ClaimsPrincipal())); - } - else if (req.Path == new PathString("/signOut")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync("Google")); - } - else if (req.Path == new PathString("/forbid")) - { - await Assert.ThrowsAsync(() => context.Authentication.ForbidAsync("Google")); - } - else if (testpath != null) - { - await testpath(context); - } - else - { - await next(); - } - }); - }, - services => - { - services.AddAuthentication(options => options.SignInScheme = TestExtensions.CookieAuthenticationScheme); - }); + return new TestServer(builder); } } } diff --git a/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs index 6100957cac..6bea890b17 100644 --- a/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/JwtBearer/JwtBearerMiddlewareTests.cs @@ -9,6 +9,7 @@ using System.Security.Claims; using System.Threading.Tasks; using System.Xml.Linq; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Http.Features.Authentication; @@ -536,67 +537,69 @@ namespace Microsoft.AspNet.Authentication.JwtBearer private static TestServer CreateServer(Action configureOptions, Func handler = null) { - return TestServer.Create(app => - { - if (configureOptions != null) + var builder = new WebApplicationBuilder() + .Configure(app => { - app.UseJwtBearerAuthentication(configureOptions); - } - - app.Use(async (context, next) => - { - if (context.Request.Path == new PathString("/checkforerrors")) + if (configureOptions != null) { - var authContext = new AuthenticateContext(Http.Authentication.AuthenticationManager.AutomaticScheme); - await context.Authentication.AuthenticateAsync(authContext); - if (authContext.Error != null) - { - throw new Exception("Failed to authenticate", authContext.Error); - } - return; + app.UseJwtBearerAuthentication(configureOptions); } - else if (context.Request.Path == new PathString("/oauth")) - { - if (context.User == null || - context.User.Identity == null || - !context.User.Identity.IsAuthenticated) - { - context.Response.StatusCode = 401; + app.Use(async (context, next) => + { + if (context.Request.Path == new PathString("/checkforerrors")) + { + var authContext = new AuthenticateContext(Http.Authentication.AuthenticationManager.AutomaticScheme); + await context.Authentication.AuthenticateAsync(authContext); + if (authContext.Error != null) + { + throw new Exception("Failed to authenticate", authContext.Error); + } return; } - - var identifier = context.User.FindFirst(ClaimTypes.NameIdentifier); - if (identifier == null) + else if (context.Request.Path == new PathString("/oauth")) { - context.Response.StatusCode = 500; + if (context.User == null || + context.User.Identity == null || + !context.User.Identity.IsAuthenticated) + { + context.Response.StatusCode = 401; - return; + return; + } + + var identifier = context.User.FindFirst(ClaimTypes.NameIdentifier); + if (identifier == null) + { + context.Response.StatusCode = 500; + + return; + } + + await context.Response.WriteAsync(identifier.Value); } - - await context.Response.WriteAsync(identifier.Value); - } - else if (context.Request.Path == new PathString("/unauthorized")) - { - // Simulate Authorization failure - var result = await context.Authentication.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme); - await context.Authentication.ChallengeAsync(JwtBearerDefaults.AuthenticationScheme); - } - else if (context.Request.Path == new PathString("/signIn")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignInAsync(JwtBearerDefaults.AuthenticationScheme, new ClaimsPrincipal())); - } - else if (context.Request.Path == new PathString("/signOut")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync(JwtBearerDefaults.AuthenticationScheme)); - } - else - { - await next(); - } - }); - }, - services => services.AddAuthentication()); + else if (context.Request.Path == new PathString("/unauthorized")) + { + // Simulate Authorization failure + var result = await context.Authentication.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme); + await context.Authentication.ChallengeAsync(JwtBearerDefaults.AuthenticationScheme); + } + else if (context.Request.Path == new PathString("/signIn")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignInAsync(JwtBearerDefaults.AuthenticationScheme, new ClaimsPrincipal())); + } + else if (context.Request.Path == new PathString("/signOut")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync(JwtBearerDefaults.AuthenticationScheme)); + } + else + { + await next(); + } + }); + }) + .ConfigureServices(services => services.AddAuthentication()); + return new TestServer(builder); } // TODO: see if we can share the TestExtensions SendAsync method (only diff is auth header) diff --git a/test/Microsoft.AspNet.Authentication.Test/MicrosoftAccount/MicrosoftAccountMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/MicrosoftAccount/MicrosoftAccountMiddlewareTests.cs index 650c86465e..1b5bd483b7 100644 --- a/test/Microsoft.AspNet.Authentication.Test/MicrosoftAccount/MicrosoftAccountMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/MicrosoftAccount/MicrosoftAccountMiddlewareTests.cs @@ -12,6 +12,7 @@ using Microsoft.AspNet.Authentication.MicrosoftAccount; using Microsoft.AspNet.Authentication.OAuth; using Microsoft.AspNet.Builder; using Microsoft.AspNet.DataProtection; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.TestHost; @@ -177,53 +178,55 @@ namespace Microsoft.AspNet.Authentication.Tests.MicrosoftAccount private static TestServer CreateServer(Action configureOptions) { - return TestServer.Create(app => - { - app.UseCookieAuthentication(options => + var builder = new WebApplicationBuilder() + .Configure(app => { - options.AuthenticationScheme = TestExtensions.CookieAuthenticationScheme; - options.AutomaticAuthenticate = true; - }); - app.UseMicrosoftAccountAuthentication(configureOptions); + app.UseCookieAuthentication(options => + { + options.AuthenticationScheme = TestExtensions.CookieAuthenticationScheme; + options.AutomaticAuthenticate = true; + }); + app.UseMicrosoftAccountAuthentication(configureOptions); - app.Use(async (context, next) => + app.Use(async (context, next) => + { + var req = context.Request; + var res = context.Response; + if (req.Path == new PathString("/challenge")) + { + await context.Authentication.ChallengeAsync("Microsoft"); + } + else if (req.Path == new PathString("/me")) + { + res.Describe(context.User); + } + else if (req.Path == new PathString("/signIn")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignInAsync("Microsoft", new ClaimsPrincipal())); + } + else if (req.Path == new PathString("/signOut")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync("Microsoft")); + } + else if (req.Path == new PathString("/forbid")) + { + await Assert.ThrowsAsync(() => context.Authentication.ForbidAsync("Microsoft")); + } + else + { + await next(); + } + }); + }) + .ConfigureServices(services => { - var req = context.Request; - var res = context.Response; - if (req.Path == new PathString("/challenge")) + services.AddAuthentication(); + services.Configure(options => { - await context.Authentication.ChallengeAsync("Microsoft"); - } - else if (req.Path == new PathString("/me")) - { - res.Describe(context.User); - } - else if (req.Path == new PathString("/signIn")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignInAsync("Microsoft", new ClaimsPrincipal())); - } - else if (req.Path == new PathString("/signOut")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync("Microsoft")); - } - else if (req.Path == new PathString("/forbid")) - { - await Assert.ThrowsAsync(() => context.Authentication.ForbidAsync("Microsoft")); - } - else - { - await next(); - } + options.SignInScheme = TestExtensions.CookieAuthenticationScheme; + }); }); - }, - services => - { - services.AddAuthentication(); - services.Configure(options => - { - options.SignInScheme = TestExtensions.CookieAuthenticationScheme; - }); - }); + return new TestServer(builder); } private static HttpResponseMessage ReturnJsonResponse(object content) diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs index 3c48657d1c..99b7040ea5 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectHandlerTests.cs @@ -12,6 +12,7 @@ using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNet.Authentication.OpenIdConnect; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.TestHost; using Microsoft.Extensions.DependencyInjection; @@ -94,8 +95,8 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect private static TestServer CreateServer(Action configureOptions, UrlEncoder encoder, OpenIdConnectHandler handler = null) { - return TestServer.Create( - app => + var builder = new WebApplicationBuilder() + .Configure(app => { var options = new OpenIdConnectOptions(); configureOptions(options); @@ -104,13 +105,13 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect { await next(); }); - }, - services => + }) + .ConfigureServices(services => { services.AddWebEncoders(); services.AddDataProtection(); - } - ); + }); + return new TestServer(builder); } } } diff --git a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs index b9f0f26f89..9446be945a 100644 --- a/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/OpenIdConnect/OpenIdConnectMiddlewareTests.cs @@ -14,6 +14,7 @@ using System.Xml.Linq; using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.OpenIdConnect; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.TestHost; @@ -380,63 +381,65 @@ namespace Microsoft.AspNet.Authentication.Tests.OpenIdConnect private static TestServer CreateServer(Action configureOptions, Func handler = null, AuthenticationProperties properties = null) { - return TestServer.Create(app => - { - app.UseCookieAuthentication(options => + var builder = new WebApplicationBuilder() + .Configure(app => { - options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme; - }); - app.UseOpenIdConnectAuthentication(configureOptions); - app.Use(async (context, next) => - { - var req = context.Request; - var res = context.Response; + app.UseCookieAuthentication(options => + { + options.AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme; + }); + app.UseOpenIdConnectAuthentication(configureOptions); + app.Use(async (context, next) => + { + var req = context.Request; + var res = context.Response; - if (req.Path == new PathString(Challenge)) - { - await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme); - } - else if (req.Path == new PathString(ChallengeWithProperties)) - { - await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, properties); - } - else if (req.Path == new PathString(ChallengeWithOutContext)) - { - res.StatusCode = 401; - } - else if (req.Path == new PathString(Signin)) - { - // REVIEW: this used to just be res.SignIn() - await context.Authentication.SignInAsync(OpenIdConnectDefaults.AuthenticationScheme, new ClaimsPrincipal()); - } - else if (req.Path == new PathString(Signout)) - { - await context.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); - } - else if (req.Path == new PathString("/signout_with_specific_redirect_uri")) - { - await context.Authentication.SignOutAsync( - OpenIdConnectDefaults.AuthenticationScheme, - new AuthenticationProperties() { RedirectUri = "http://www.example.com/specific_redirect_uri" }); - } - else if (handler != null) - { - await handler(context); - } - else - { - await next(); - } - }); - }, - services => - { - services.AddAuthentication(); - services.Configure(options => + if (req.Path == new PathString(Challenge)) + { + await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme); + } + else if (req.Path == new PathString(ChallengeWithProperties)) + { + await context.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, properties); + } + else if (req.Path == new PathString(ChallengeWithOutContext)) + { + res.StatusCode = 401; + } + else if (req.Path == new PathString(Signin)) + { + // REVIEW: this used to just be res.SignIn() + await context.Authentication.SignInAsync(OpenIdConnectDefaults.AuthenticationScheme, new ClaimsPrincipal()); + } + else if (req.Path == new PathString(Signout)) + { + await context.Authentication.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme); + } + else if (req.Path == new PathString("/signout_with_specific_redirect_uri")) + { + await context.Authentication.SignOutAsync( + OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties() { RedirectUri = "http://www.example.com/specific_redirect_uri" }); + } + else if (handler != null) + { + await handler(context); + } + else + { + await next(); + } + }); + }) + .ConfigureServices(services => { - options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + services.AddAuthentication(); + services.Configure(options => + { + options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + }); }); - }); + return new TestServer(builder); } private static async Task SendAsync(TestServer server, string uri, string cookieHeader = null) diff --git a/test/Microsoft.AspNet.Authentication.Test/Twitter/TwitterMiddlewareTests.cs b/test/Microsoft.AspNet.Authentication.Test/Twitter/TwitterMiddlewareTests.cs index 668f874811..db77d7f5ff 100644 --- a/test/Microsoft.AspNet.Authentication.Test/Twitter/TwitterMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Authentication.Test/Twitter/TwitterMiddlewareTests.cs @@ -1,17 +1,16 @@ // Copyright (c) .NET Foundation. All rights reserved. See License.txt in the project root for license information. using System; -using System.Linq; using System.Net; using System.Net.Http; using System.Security.Claims; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.TestHost; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.WebEncoders; using Xunit; namespace Microsoft.AspNet.Authentication.Twitter @@ -153,43 +152,45 @@ namespace Microsoft.AspNet.Authentication.Twitter private static TestServer CreateServer(Action configure, Func handler = null) { - return TestServer.Create(app => - { - app.UseCookieAuthentication(options => + var builder = new WebApplicationBuilder() + .Configure(app => { - options.AuthenticationScheme = "External"; - }); - app.UseTwitterAuthentication(configure); - app.Use(async (context, next) => + app.UseCookieAuthentication(options => + { + options.AuthenticationScheme = "External"; + }); + app.UseTwitterAuthentication(configure); + app.Use(async (context, next) => + { + var req = context.Request; + var res = context.Response; + if (req.Path == new PathString("/signIn")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignInAsync("Twitter", new ClaimsPrincipal())); + } + else if (req.Path == new PathString("/signOut")) + { + await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync("Twitter")); + } + else if (req.Path == new PathString("/forbid")) + { + await Assert.ThrowsAsync(() => context.Authentication.ForbidAsync("Twitter")); + } + else if (handler == null || !handler(context)) + { + await next(); + } + }); + }) + .ConfigureServices(services => { - var req = context.Request; - var res = context.Response; - if (req.Path == new PathString("/signIn")) + services.AddAuthentication(); + services.Configure(options => { - await Assert.ThrowsAsync(() => context.Authentication.SignInAsync("Twitter", new ClaimsPrincipal())); - } - else if (req.Path == new PathString("/signOut")) - { - await Assert.ThrowsAsync(() => context.Authentication.SignOutAsync("Twitter")); - } - else if (req.Path == new PathString("/forbid")) - { - await Assert.ThrowsAsync(() => context.Authentication.ForbidAsync("Twitter")); - } - else if (handler == null || !handler(context)) - { - await next(); - } + options.SignInScheme = "External"; + }); }); - }, - services => - { - services.AddAuthentication(); - services.Configure(options => - { - options.SignInScheme = "External"; - }); - }); + return new TestServer(builder); } } } diff --git a/test/Microsoft.AspNet.CookiePolicy.Test/CookiePolicyTests.cs b/test/Microsoft.AspNet.CookiePolicy.Test/CookiePolicyTests.cs index 78f20c9cf1..4b798c5613 100644 --- a/test/Microsoft.AspNet.CookiePolicy.Test/CookiePolicyTests.cs +++ b/test/Microsoft.AspNet.CookiePolicy.Test/CookiePolicyTests.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features.Internal; @@ -129,18 +130,20 @@ namespace Microsoft.AspNet.CookiePolicy.Test [Fact] public async Task CookiePolicyCanHijackAppend() { - var server = TestServer.Create(app => - { - app.UseCookiePolicy(options => options.OnAppendCookie = ctx => ctx.CookieName = ctx.CookieValue = "Hao"); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - context.Response.Cookies.Append("A", "A"); - context.Response.Cookies.Append("B", "B", new CookieOptions { Secure = false }); - context.Response.Cookies.Append("C", "C", new CookieOptions()); - context.Response.Cookies.Append("D", "D", new CookieOptions { Secure = true }); - return Task.FromResult(0); + app.UseCookiePolicy(options => options.OnAppendCookie = ctx => ctx.CookieName = ctx.CookieValue = "Hao"); + app.Run(context => + { + context.Response.Cookies.Append("A", "A"); + context.Response.Cookies.Append("B", "B", new CookieOptions { Secure = false }); + context.Response.Cookies.Append("C", "C", new CookieOptions()); + context.Response.Cookies.Append("D", "D", new CookieOptions { Secure = true }); + return Task.FromResult(0); + }); }); - }); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/login"); @@ -154,7 +157,8 @@ namespace Microsoft.AspNet.CookiePolicy.Test [Fact] public async Task CookiePolicyCanHijackDelete() { - var server = TestServer.Create(app => + var builder = new WebApplicationBuilder() + .Configure(app => { app.UseCookiePolicy(options => options.OnDeleteCookie = ctx => ctx.CookieName = "A"); app.Run(context => @@ -166,6 +170,7 @@ namespace Microsoft.AspNet.CookiePolicy.Test return Task.FromResult(0); }); }); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/login"); @@ -177,7 +182,8 @@ namespace Microsoft.AspNet.CookiePolicy.Test [Fact] public async Task CookiePolicyCallsCookieFeature() { - var server = TestServer.Create(app => + var builder = new WebApplicationBuilder() + .Configure(app => { app.Use(next => context => { @@ -194,6 +200,7 @@ namespace Microsoft.AspNet.CookiePolicy.Test return context.Response.WriteAsync("Done"); }); }); + var server = new TestServer(builder); var transaction = await server.SendAsync("http://example.com/login"); Assert.Equal("Done", transaction.ResponseText); @@ -251,7 +258,8 @@ namespace Microsoft.AspNet.CookiePolicy.Test RequestDelegate configureSetup, params RequestTest[] tests) { - var server = TestServer.Create(app => + var builder = new WebApplicationBuilder() + .Configure(app => { app.Map(path, map => { @@ -259,6 +267,7 @@ namespace Microsoft.AspNet.CookiePolicy.Test map.Run(configureSetup); }); }); + var server = new TestServer(builder); foreach (var test in tests) { await test.Execute(server); diff --git a/test/Microsoft.Owin.Security.Cookies.Interop.Test/TicketInteropTests.cs b/test/Microsoft.Owin.Security.Cookies.Interop.Test/TicketInteropTests.cs index 725c5b1f2f..71acba5978 100644 --- a/test/Microsoft.Owin.Security.Cookies.Interop.Test/TicketInteropTests.cs +++ b/test/Microsoft.Owin.Security.Cookies.Interop.Test/TicketInteropTests.cs @@ -13,6 +13,7 @@ using System.Xml; using System.Xml.Linq; using Microsoft.AspNet.Authentication; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Owin; using Microsoft.Owin.Security.Cookies; @@ -120,15 +121,18 @@ namespace Microsoft.AspNet.CookiePolicy.Test var transaction = await SendAsync(interopServer, "http://example.com"); - var newServer = TestHost.TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.DataProtectionProvider = dataProtection); - app.Run(async context => + var builder = new WebApplicationBuilder() + .Configure(app => { - var result = await context.Authentication.AuthenticateAsync("Cookies"); - await context.Response.WriteAsync(result.Identity.Name); - }); - }, services => services.AddAuthentication()); + app.UseCookieAuthentication(options => options.DataProtectionProvider = dataProtection); + app.Run(async context => + { + var result = await context.Authentication.AuthenticateAsync("Cookies"); + await context.Response.WriteAsync(result.Identity.Name); + }); + }) + .ConfigureServices(services => services.AddAuthentication()); + var newServer = new TestHost.TestServer(builder); var request = new HttpRequestMessage(HttpMethod.Get, "http://example.com/login"); request.Headers.Add("Cookie", transaction.SetCookie.Split(new[] { ';' }, 2).First()); @@ -146,11 +150,14 @@ namespace Microsoft.AspNet.CookiePolicy.Test user.AddIdentity(identity); var dataProtection = new DataProtection.DataProtectionProvider(new DirectoryInfo("..\\..\\artifacts")); - var newServer = TestHost.TestServer.Create(app => - { - app.UseCookieAuthentication(options => options.DataProtectionProvider = dataProtection); - app.Run(context => context.Authentication.SignInAsync("Cookies", user)); - }, services => services.AddAuthentication()); + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseCookieAuthentication(options => options.DataProtectionProvider = dataProtection); + app.Run(context => context.Authentication.SignInAsync("Cookies", user)); + }) + .ConfigureServices(services => services.AddAuthentication()); + var newServer = new TestHost.TestServer(builder); var cookie = await SendAndGetCookie(newServer, "http://example.com/login");