使用RunImpersonated进行HttpClient调用无法进行NUnit测试,但可以在控制台中使用 [英] Using RunImpersonated for an HttpClient call fails for a NUnit test, but works in Console

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

问题描述

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

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?

注意:完整的repro代码可以在这里找到: 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.

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

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