使用TestServer时如何传递和访问ClaimsPrincipal标识

Pat*_*lan 5 xunit dotnet-httpclient .net-core asp.net-core

我有一个aspnetcore Web API,并使用以下代码为Windows Auth配置主机.

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();
Run Code Online (Sandbox Code Playgroud)

调用客户端配置如下

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"));
Run Code Online (Sandbox Code Playgroud)

在我正在呼叫的服务中,我可以访问主叫用户的ClaimsPrincipal身份.

我的问题是如何使用通过CreateClient方法初始化的TestServer Client从集成测试中调用此服务.从集成测试调用时,确保设置标识的最佳方法是什么?

我也在使用XUnit,以防你想知道.

任何帮助或建议将不胜感激.