对于 NUnit 测试,将 RunImpersonated 用于 HttpClient 调用失败,但在控制台中有效 [英] Using RunImpersonated for an HttpClient call fails for a NUnit test, but works in Console

查看:23
本文介绍了对于 NUnit 测试,将 RunImpersonated 用于 HttpClient 调用失败,但在控制台中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的测试作为测试帐户运行.为此,我设置了以下代码以在我的测试帐户中创建句柄:

I need to have my tests run as a testing account. To accomplish that I setup to the following code to create a handle into my testing account:

SafeAccessTokenHandle testAccountHandle;

bool returnValue = LogonUser("TestAccount", "myDom.net", 
       "pass", 2, 0, out testAccountHandle);

然后我可以调用加载 URL:

I can then make a call to load a URL:

HttpResponseMessage response = null;

await WindowsIdentity.RunImpersonated<Task>(testAccountHandle, async () =>
{
    var url = "https://accounts.google.com/.well-known/openid-configuration";
    response = await httpClient.GetAsync(url);
});
testAccountHandle.Dispose();

当我在控制台应用程序中运行它时,它工作得很好.(在 LinqPad 中也是如此.)

When I run this in a console application, it works just fine. (Likewise in LinqPad.)

但是,当我在 NUnit 测试中运行此代码时,出现以下错误:

However when I run this code in an NUnit test, I get the following error:

System.Net.Sockets.SocketException :这通常是主机名解析过程中的临时错误,意味着本地服务器没有收到来自权威服务器的响应.

System.Net.Sockets.SocketException : This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server.

它说这通常是一个临时错误,但每次我在 NUnit 测试中模拟运行时都会发生,而在控制台中运行时则不会.如果我没有模拟运行,当我在 NUnit 测试中运行时,它也永远不会发生.(简而言之,它只发生在 NUnit 测试和模拟中.)

It says it is usually a temporary error, but it happens every single time I run impersonated in the NUnit Test, and never when I run in the Console. It also never happens when I run in the NUnit test if I am not running impersonated. (In short it ONLY happens when in an NUnit Test and Impersonated.)

我不知道如何调试这个.很明显 NUnit 不喜欢我的模仿,但我不知道该怎么做.

I am not sure how to go about debugging this. It seems clear that NUnit does not like my impersonation, but I am not sure what to do about it.

如何在 NUnit 测试中使用 RunImpersonated 时成功调用 HttpClient.GetAsync?

How can I make a successful HttpClient.GetAsync call while using RunImpersonated in an NUnit test?

注意:完整的重现代码可以在这里找到:https://github.com/nunit/nunit/issues/3672

NOTE: Full repro code can be found here: https://github.com/nunit/nunit/issues/3672

推荐答案

这似乎是 .NET Core 的错误.在此处查看未解决的问题和讨论:https://github.com/dotnet/runtime/issues/29935

This appears to be a bug with .NET Core. See the open issue and discussion here: https://github.com/dotnet/runtime/issues/29935

与 .NET Framework 相比,WindowsIdentity.RunImpersonated() 在 .NET Core 中的工作方式似乎有所不同.这会导致各种问题,包括影响 ASP.NET Core 的问题,#29351.

It seems that WindowsIdentity.RunImpersonated() works differently in .NET Core compared with .NET Framework. This is causing a variety of issues including one affecting ASP.NET Core, #29351.

在模拟令牌上设置身份令牌权限的方式存在一些差异.这导致访问被拒绝"以各种方式提出问题.

There is some difference in the way that the identity token permissions are getting set on the impersonated token. This is causing "access denied" issues in a variety of ways.

解决方法

来自 ASP.NET Core 的问题 #29351 引用了这个确切的错误消息并包含一种解决方法.将环境变量 DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER 设置为 0 会禁用 SocketsHttpHandler 并使这个问题消失.例如,以下工作没有错误:

Workaround

Issue #29351 from ASP.NET Core references this exact error message and contains a workaround. Setting the environment variable DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER to 0 disables the SocketsHttpHandler and makes this problem go away. For example, the following works without the error:

Environment.SetEnvironmentVariable("DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER", "0");
HttpResponseMessage response = null;

await WindowsIdentity.RunImpersonated<Task>(testAccountHandle, async () =>
{
    var url = "https://accounts.google.com/.well-known/openid-configuration";
    response = await httpClient.GetAsync(url);
});

您可能需要考虑仅为此特定测试而不是每个测试设置此环境变量.我不确定禁用 SocketsHttpHandler 的影响是什么,因此请自担风险使用变通方法.

You may need to make considerations for only setting this environment variable for this specific test and not for every test. I'm not sure what the impacts of disabling the SocketsHttpHandler are, so use the workaround at your own risk.

这篇关于对于 NUnit 测试,将 RunImpersonated 用于 HttpClient 调用失败,但在控制台中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆