public static TestClaimsProvider WithUserClaims()
        {
            var provider = new TestClaimsProvider();

            provider.Claims.Add(new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString()));
            provider.Claims.Add(new Claim(ClaimTypes.Name, "User"));

            return(provider);
        }
        public static HttpClient CreateClientWithTestAuth <T>(this WebApplicationFactory <T> factory,
                                                              TestClaimsProvider claimsProvider) where T : class
        {
            var client = factory.WithAuthentication(claimsProvider).CreateClient(new WebApplicationFactoryClientOptions
            {
                AllowAutoRedirect = false
            });

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Test");

            return(client);
        }
        public static WebApplicationFactory <T> WithAuthentication <T>(this WebApplicationFactory <T> factory,
                                                                       TestClaimsProvider claimsProvider) where T : class
        {
            return(factory.WithWebHostBuilder(builder =>
            {
                builder.ConfigureTestServices(services =>
                {
                    services.AddAuthentication("Test")
                    .AddScheme <AuthenticationSchemeOptions, TestAuthHandler>("Test", op => { });

                    services.AddScoped(_ => claimsProvider);
                });
            }));
        }
Ejemplo n.º 4
0
 public TestAuthHandler(IOptionsMonitor <AuthenticationSchemeOptions> options, ILoggerFactory logger,
                        UrlEncoder encoder, ISystemClock clock, TestClaimsProvider claimsProvider) : base(options, logger, encoder, clock)
 {
     _claims = claimsProvider.Claims;
 }