Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to pass and access ClaimsPrincipal Identity when using TestServer

$
0
0

I have an aspnetcore Web API and the host is configured for Windows Auth with the following code.

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls("http://TestServer:5000")
.UseStartup<Startup>()
.UseWebListener(options =>
{
options.Listener.AuthenticationManager.AuthenticationSchemes = Microsoft.Net.Http.Server.AuthenticationSchemes.NTLM;
//NOTE: Multiple auth assignments don't work in 1.0.0 - Wait for 1.1.* for the line below to start working
//See: https://github.com/aspnet/Announcements/issues/198
//options.Listener.AuthenticationManager.AllowAnonymous = true;
})
.Build();

host.Run();

The calling Client is configured with the following

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

HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("http://TestServer:5000/");
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));

In the service I'm calling I can access the calling user's ClaimsPrincipal Identity.

My Question is how do I call this service from an integration test using the TestServer Client initialised via the CreateClient method. What is the best way to ensure the Identity is set when calling from an integration test?

I'm also using XUnit in case you would like to know.

Any help or advice would be greatly appreciated.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>