I've been following the official documentation to mock the authorization service, and it seems to be working perfectly when using JWTBearer authentication, but it returns a 500 status code when I try using Negotiate instead.
Working Program.cs
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer();
NOT working Program.cs
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
Test code
...
HttpClient client = _factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
services.AddAuthentication("Test")
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => { });
});
})
.CreateClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(scheme: "Test");
var response = await client.GetAsync("/my-endpoint"); // HTTP status is 200 with JWTBearer but 500 with Negotiate
...
TestAuthHandler.cs
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, "Test");
AuthenticateResult result = AuthenticateResult.Success(ticket);
return Task.FromResult(result);
}
}