10

I'm using the AspNetCore TestServer and trying to connect to an API service configured for NTLM Authentication. The service I'm connecting to needs to access the System.Security.Claims.ClaimsPrincipal.Identity.

From my Console application I can initialise he HTTPClient as follows and it works

HttpClientHandler handler = new HttpClientHandler()
{
    UseDefaultCredentials = true,
    PreAuthenticate = true
};

HttpClient client = new HttpClient(handler);

However, the TestServer CreateClient method does not accept a HttpClientHandler. So how do I configure to use UseDefaultCredentials?

PatrickNolan
  • 1,671
  • 2
  • 20
  • 40
  • Did you have it figured out? I ran into this situation now, cuz of Digital Signature requirement here. – fletchsod Oct 06 '17 at 15:41
  • You can use FlurlClient with asp.net core TestServer https://stackoverflow.com/questions/50155702/can-i-use-flurlclient-with-asp-net-core-testserver – Michael Freidgeim Nov 17 '18 at 04:04

3 Answers3

0

An option is to create an overloaded constructor on your HttpClientHandler and wrap the TestServer's HttpClientHandler. There, from the SendAsync method you just need to Invoke the method SendAsync of he TestServer's  HttpClientHandler like this

if (_wrappedMessageHandler != null)
{
    var method = typeof(HttpMessageHandler).GetMethod("SendAsync", BindingFlags.Instance | BindingFlags.NonPublic);
    var result = method.Invoke(_wrappedMessageHandler, new object[] { request, cancellationToken });
    return await (Task<HttpResponseMessage>)result;
}
else
{
    var response = await base.SendAsync(request, cancellationToken);
    return response;

}
cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
Sotiris
  • 52
  • 3
0

You can't do windows auth with the test server. A related feature was added for an upcoming release. https://github.com/aspnet/Hosting/pull/1248. See the linked issues and PRs for workarounds via middleware.

Tratcher
  • 5,929
  • 34
  • 44
0

To resolve this issue I inject the user throught a middleware in my test:

public class FakeIdentity : IIdentity
{
    public string AuthenticationType => "fake";

    public bool IsAuthenticated => true;

    public string Name => "fake";
}

public class TestUtils
{
    public static TestServer CreateTestServer()
    {
        Startup startup = null;
        var webHostBuilder = new WebHostBuilder()
            .ConfigureServices((context, services) =>
            {
                startup = new Startup(context.Configuration);
                startup.ConfigureServices(services);
            })
            .Configure(builder =>
            {
            var services = builder.ApplicationServices;
            using (var scope = services.CreateScope())
            {
                builder.Use(async (context, next) =>
                {
                    context.User = new ClaimsPrincipal(new ClaimsIdentity(new FakeIdentity(), new List<Claim>
                    {
                                // a list of claims the application need
                            }));
                    await next();
                });
                startup.Configure(builder, services.GetRequiredService<IHostingEnvironment>());
            });

        var testServer = new TestServer(webHostBuilder);

        return testServer;
    }
}
agua from mars
  • 16,428
  • 4
  • 61
  • 70