我在.NET Framework 4.0程序集中具有以下内容:
var newId= new WindowsIdentity(duplicateTokenHandle);
WindowsImpersonationContext newId = ImpersonatedIdentity.Impersonate();
Run Code Online (Sandbox Code Playgroud)
我将它移植到ASP.Core,但WindowsImpersonationContext并WindowsIdentity.Impersonate()没有找到。
我尝试添加以下内容:
System.Security.Claims 4.3.0System.Security.Principal 4.3.0System.Security.Principal.Windows 4.4.0如何在ASP.Core中执行模拟?
更新资料
看起来.NET Core或.NET Standard不支持它-是否有解决方法,或者我必须辞职才能针对框架吗?
我正在尝试在.NET Core 2.1 Web-API中进行模拟。因此,此Web-API使用HttpClient调用了另一个Web-API,我需要调用第一个的用户也必须是正在执行第二个的用户。同样的情况也可以在通过此调用运行完整框架的另一个Web-API中使用:
((WindowsIdentity)_httpContextAccessor.HttpContext.User.Identity).Impersonate()
Run Code Online (Sandbox Code Playgroud)
由于Impersonate().NET Core 2.1中没有此功能,因此我使用搜索了一些示例,WindowsIdentity.RunImpersonated并尝试了与此类似的不同版本的代码:
WindowsIdentity identity = (WindowsIdentity)m_contextAccessor.HttpContext.User.Identity;
HttpClient client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true });
await WindowsIdentity.RunImpersonated(identity.AccessToken, async () =>
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
var response = await client.SendAsync(request);
});
Run Code Online (Sandbox Code Playgroud)
这将引发错误,client.SendAsync错误消息是这样的:
在此调用仍在处理期间,已对WSALookupServiceEnd进行了调用。呼叫已被取消---> System.Net.Http.HttpRequestException
堆栈跟踪的开始:
在System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32端口,CancellationToken cancelleToken)-内部异常堆栈跟踪的结尾-在System.Net.Http.ConnectHelper.ConnectAsync(字符串主机,Int32端口,CancellationToken System.Threading.Tasks.ValueTask`1.get_Result()处的System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage请求,CancellationToken cancelleToken)
是否有其他人看到此错误或对如何解决此问题有任何见解?我尝试了不同版本的代码来RunImpersonated与HttpContext用户调用,并且都导致了相同的错误。
感谢您的输入