private HttpClient CreateHttpClient(string user = null, string role = null) { var factory = new TestAppFactory(); factory.ConfigureServices(services => { services.AddControllers(o => { o.Conventions.Add(new DefaultAuthorizeActionModelConvention(null)); }); services.AddAuthenticationCore(o => { o.DefaultScheme = "None"; }); services.AddSingleton <UrlEncoder, UrlTestEncoder>(); services.AddSingleton <ISystemClock, TestClock>(); var b = new AuthenticationBuilder(services); b.AddScheme <AutoAuthenticationTestSchemeOptions, AutoAuthenticationTestScheme>("None", o => { if (user != null) { o.UserName = user; o.Role = role; } }); services.AddAuthorization(); services.AddLogging(); } ); factory.ConfigureBuilder(app => { app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(e => e.MapControllers()); } ); return(factory.CreateClient(new WebApplicationFactoryClientOptions { BaseAddress = new Uri("https://example.test", UriKind.Absolute), AllowAutoRedirect = false } )); }
private HttpClient CreateClient( out TestAppFactory factory, int passwordSize = 17, string schemeName = null, Func <PersonalAccessTokenValidatePrincipalContext <TestUser>, Task> validatedPrincipal = null) { Dictionary <int, string> storedHashes = new Dictionary <int, string>(); void ConfigureOptions(PersonalAccessTokenAuthenticationOptions <TestUser> o) { o.Events = new PersonalAccessTokenEvents <TestUser> { OnGetTokenHash = context => { TestUser user = GetUser(context.TokenId); if (user != null) { string hash = storedHashes.GetValueOrDefault( context.TokenId, TestHasher.CalculateHash(user, GetPasswordForToken(context.TokenId, passwordSize)) ); context.Success(hash, user); } return(Task.CompletedTask); }, OnSetTokenHash = context => { storedHashes.Add(context.User.Id, context.Hash); return(Task.FromResult(context.User.Id)); }, }; if (validatedPrincipal != null) { o.Events.OnValidatePrincipal = validatedPrincipal; } o.PasswordSize = passwordSize; } void ConfigureAuth(AuthenticationBuilder b) { if (schemeName == null) { b.AddPersonalAccessToken <TestUser>(ConfigureOptions); } else { b.AddPersonalAccessToken <TestUser>(schemeName, $"Display {schemeName}", ConfigureOptions); } } var localClock = new TestClock(); factory = new TestAppFactory(_output); factory.ConfigureServices(services => { services.AddSingleton <ISystemClock>(localClock); services.AddSingleton <IPasswordHasher <TestUser>, TestHasher>(); services.AddControllers(); services.AddAuthenticationCore(o => o.DefaultScheme = schemeName ?? PersonalAccessTokenDefaults.AuthenticationScheme); ConfigureAuth(new AuthenticationBuilder(services)); services.AddAuthorization(); services.AddIdentityCore <TestUser>(); services.AddSingleton <UrlEncoder, UrlTestEncoder>(); services.AddSingleton <SignInManager <TestUser> >(); var userStore = new Mock <IUserStore <TestUser> >(); services.AddSingleton(userStore.Object); services.AddHttpContextAccessor(); services.AddSingleton <IUserClaimsPrincipalFactory <TestUser>, TestClaimsFactory>(); }); factory.ConfigureBuilder(app => { app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(e => e.MapControllers()); }); HttpClient client = factory.CreateClient(new WebApplicationFactoryClientOptions { BaseAddress = new Uri("https://example.test", UriKind.Absolute), AllowAutoRedirect = false } ); return(client); }