具有 Windows 身份验证的 HttpRequestMessage

HttpRequestMessage with Windows Authentication

我正在尝试对启用了 Windows 身份验证的 ASP.Net 核心应用程序进行一些集成测试。对于具有 [Authorize] 属性的控制器方法,我需要通过请求中的当前 windows 身份发送。

有很多关于如何使用旧的 HttpWebRequest 方法执行此操作的信息,但我找不到有关通过 HttpRequestMessage 执行此操作的信息。大概我必须在身份验证 header 中通过当前用户进行编码和发送?有人可以帮忙吗?

Asp.Net Core 不会为您做模拟。您需要致电 WindowsIdentity.Impersonate (https://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.110).aspx) to apply the given identity to the current thread. Then you set up HttpClient with UseDefaultCredentials: