|
5 | 5 | using System.Diagnostics;
|
6 | 6 | using System.Diagnostics.Tracing;
|
7 | 7 | using System.Net;
|
| 8 | +using System.Net.Http; |
8 | 9 | using System.Reflection;
|
| 10 | +using System.Security.Claims; |
9 | 11 | using System.Text;
|
| 12 | +using System.Text.Encodings.Web; |
| 13 | +using Microsoft.AspNetCore.Authentication; |
| 14 | +using Microsoft.AspNetCore.Authorization; |
10 | 15 | using Microsoft.AspNetCore.Builder;
|
11 | 16 | using Microsoft.AspNetCore.HostFiltering;
|
12 | 17 | using Microsoft.AspNetCore.Hosting;
|
@@ -1976,6 +1981,73 @@ public void CanObserveDefaultServicesInServiceCollection()
|
1976 | 1981 | Assert.Contains(builder.Services, service => service.ServiceType == typeof(ILogger<>));
|
1977 | 1982 | }
|
1978 | 1983 |
|
| 1984 | + [Fact] |
| 1985 | + public async Task RegisterAuthMiddlewaresCorrectly() |
| 1986 | + { |
| 1987 | + var helloEndpointCalled = false; |
| 1988 | + var customMiddlewareExecuted = false; |
| 1989 | + var username = "foobar"; |
| 1990 | + |
| 1991 | + var builder = WebApplication.CreateBuilder(); |
| 1992 | + builder.Services.AddAuthenticationCore(o => |
| 1993 | + { |
| 1994 | + o.DefaultScheme = "testSchemeName"; |
| 1995 | + }); |
| 1996 | + builder.Authentication.AddScheme<AuthenticationSchemeOptions, UberHandler>("testSchemeName", "testDisplayName", _ => { }); |
| 1997 | + builder.WebHost.UseTestServer(); |
| 1998 | + await using var app = builder.Build(); |
| 1999 | + |
| 2000 | + app.Use(next => |
| 2001 | + { |
| 2002 | + return async context => |
| 2003 | + { |
| 2004 | + // IAuthenticationFeature is added by the authentication middleware |
| 2005 | + // during invocation. This middleware should run after authentication |
| 2006 | + // and be able to access the feature. |
| 2007 | + var authFeature = context.Features.Get<IAuthenticationFeature>(); |
| 2008 | + Assert.NotNull(authFeature); |
| 2009 | + customMiddlewareExecuted = true; |
| 2010 | + Assert.Equal(username, context.User.Identity.Name); |
| 2011 | + await next(context); |
| 2012 | + }; |
| 2013 | + }); |
| 2014 | + |
| 2015 | + app.MapGet("/hello", (ClaimsPrincipal user) => |
| 2016 | + { |
| 2017 | + helloEndpointCalled = true; |
| 2018 | + Assert.Equal(username, user.Identity.Name); |
| 2019 | + }).AllowAnonymous(); |
| 2020 | + |
| 2021 | + await app.StartAsync(); |
| 2022 | + var client = app.GetTestClient(); |
| 2023 | + await client.GetStringAsync($"/hello?username={username}"); |
| 2024 | + |
| 2025 | + Assert.True(helloEndpointCalled); |
| 2026 | + Assert.True(customMiddlewareExecuted); |
| 2027 | + } |
| 2028 | + |
| 2029 | + private class UberHandler : AuthenticationHandler<AuthenticationSchemeOptions> |
| 2030 | + { |
| 2031 | + public UberHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) { } |
| 2032 | + |
| 2033 | + protected override Task HandleChallengeAsync(AuthenticationProperties properties) => Task.CompletedTask; |
| 2034 | + |
| 2035 | + protected override Task HandleForbiddenAsync(AuthenticationProperties properties) => Task.CompletedTask; |
| 2036 | + |
| 2037 | + public Task<bool> HandleRequestAsync() => Task.FromResult(false); |
| 2038 | + |
| 2039 | + protected override Task<AuthenticateResult> HandleAuthenticateAsync() |
| 2040 | + { |
| 2041 | + var username = Request.Query["username"]; |
| 2042 | + var principal = new ClaimsPrincipal(); |
| 2043 | + var id = new ClaimsIdentity(); |
| 2044 | + id.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, username)); |
| 2045 | + principal.AddIdentity(id); |
| 2046 | + return Task.FromResult(AuthenticateResult.Success( |
| 2047 | + new AuthenticationTicket(principal, "custom"))); |
| 2048 | + } |
| 2049 | + } |
| 2050 | + |
1979 | 2051 | public class RandomConfigurationSource : IConfigurationSource
|
1980 | 2052 | {
|
1981 | 2053 | public int ProvidersBuilt { get; set; }
|
|
0 commit comments