private static void ConfigureApplication <TOptions>(IApplicationBuilder app, OAuthTests <TOptions> tests) where TOptions : OAuthOptions { // Configure a single HTTP resource that challenges the client if unauthenticated // or returns the logged in user's claims as XML if the request is authenticated. tests.ConfigureApplication(app); app.UseAuthentication(); app.Map("/me", childApp => childApp.Run( async context => { if (context.User.Identity.IsAuthenticated) { string xml = IdentityToXmlString(context.User); byte[] buffer = Encoding.UTF8.GetBytes(xml.ToString()); context.Response.StatusCode = 200; context.Response.ContentType = "text/xml"; await context.Response.Body.WriteAsync(buffer, 0, buffer.Length); } else { await context.ChallengeAsync(); } })); }
private static void Configure <TOptions>(IWebHostBuilder builder, OAuthTests <TOptions> tests) where TOptions : OAuthOptions { // Use a dummy content root builder.UseContentRoot("."); // Route application logs to xunit output for debugging builder.ConfigureLogging(logging => { logging.AddXUnit(tests) .SetMinimumLevel(LogLevel.Information); }); // Configure the test application builder.Configure(app => ConfigureApplication(app, tests)) .ConfigureServices(services => { // Allow HTTP requests to external services to be intercepted services.AddHttpClient(); services.AddSingleton <IHttpMessageHandlerBuilderFilter, HttpRequestInterceptionFilter>( (_) => new HttpRequestInterceptionFilter(tests.Interceptor)); // Set up the test endpoint services.AddRouting(); // Configure authentication var authentication = services .AddAuthentication("External") .AddCookie("External", o => o.ForwardChallenge = tests.DefaultScheme); tests.RegisterAuthentication(authentication); }); }
private static void ConfigureApplication <TOptions>(IApplicationBuilder app, OAuthTests <TOptions> tests) where TOptions : OAuthOptions { tests.ConfigureApplication(app); // Configure a single HTTP resource that challenges the client if unauthenticated // or returns the logged in user's claims as XML if the request is authenticated. app.UseRouting(); app.UseAuthentication() .UseAuthorization() .UseEndpoints(endpoints => { endpoints.MapGet( "/me", async context => { if (context.User.Identity?.IsAuthenticated == true) { string xml = IdentityToXmlString(context.User); byte[] buffer = Encoding.UTF8.GetBytes(xml); context.Response.StatusCode = 200; context.Response.ContentType = "text/xml"; await context.Response.Body.WriteAsync(buffer, context.RequestAborted); } else { await tests.ChallengeAsync(context); } }); }); }
/// <summary> /// Creates a test application for the specified type of authentication. /// </summary> /// <typeparam name="TOptions">The type of the configuration options for the authentication provider.</typeparam> /// <param name="tests">The test class to configure the application for.</param> /// <param name="configureServices">An optional delegate to configure additional application services.</param> /// <returns> /// The test application to use for the authentication provider. /// </returns> public static WebApplicationFactory <Program> CreateApplication <TOptions>(OAuthTests <TOptions> tests, Action <IServiceCollection> configureServices = null) where TOptions : OAuthOptions { return(new TestApplicationFactory() .WithWebHostBuilder(builder => { Configure(builder, tests); if (configureServices != null) { builder.ConfigureServices(configureServices); } })); }
/// <summary> /// Creates a test application for the specified type of authentication. /// </summary> /// <typeparam name="TOptions">The type of the configuration options for the authentication provider.</typeparam> /// <param name="tests">The test class to configure the application for.</param> /// <param name="configureServices">An optional delegate to configure additional application services.</param> /// <returns> /// The test application to use for the authentication provider. /// </returns> public static WebApplicationFactory <Program> CreateApplication <TOptions>(OAuthTests <TOptions> tests, Action <IServiceCollection>?configureServices = null) where TOptions : OAuthOptions, new() { #pragma warning disable CA2000 return(new TestApplicationFactory() .WithWebHostBuilder(builder => { Configure(builder, tests); if (configureServices != null) { builder.ConfigureServices(configureServices); } })); #pragma warning restore CA2000 }