There is a much better way to do this my dudes :) and its supported way. check this out, we can implement IServerIntegratedAuth
public class TestServerIntegratedAuth : IServerIntegratedAuth
{
public bool IsEnabled => true;
public string AuthenticationScheme => "TestScheme";
}
this will instruct NegotiateHandles to forward authentication to TestScheme, then in tests you can add this IServerIntegratedAuth implementation to a container and register TestScheme if you want to perform authentication
var client = factory.WithWebHostBuilder(b =>
{
b.ConfigureServices(services =>
{
services.AddSingleton<IServerIntegratedAuth, TestServerIntegratedAuth>();
services
.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("TestScheme", options => { });
});
}).CreateDefaultClient();
this TestAuthHandler is from ms docs
public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public TestAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var claims = new[] { new Claim(ClaimTypes.Name, "Test user") };
var identity = new ClaimsIdentity(claims, "Test");
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, "TestScheme");
var result = AuthenticateResult.Success(ticket);
return Task.FromResult(result);
}
}
and that's it. Works like a charm.
The same thing IIS integration implements in order to perform negotiation natively.