【发布时间】:2020-07-05 11:13:37
【问题描述】:
我有以下代码:
var baseUrl = "https://" + GetIdentityProviderHost(environment) + "/oauth2/authorize";
var query = $"?scope=openid&response_type=code&redirect_uri={redirectUrl}&client_id={clientId}";
var combinedUrl = baseUrl + query;
var currentUser = WindowsIdentity.GetCurrent();
await WindowsIdentity.RunImpersonated(currentUser.AccessToken, async() =>
{
using (var client = new WebClient{ UseDefaultCredentials = true })
{
var response = client.DownloadString(combinedUrl);
Console.WriteLine(response);
}
});
它基本上是构造一个 URL,然后调用它。
调用返回 401(未授权)。
但如果我将 combinedUrl 粘贴到 chrome 或 postman 中,它可以完美运行。这告诉我,我的通话可以正常工作,因为 Chrome 正在使用我的 Windows 凭据进行通话。
我添加了WindowsIdentity.RunImpersonated 代码来尝试解决这个问题。不过好像没什么效果。
如何使用集成 Windows 身份验证 (IWA) 拨打网络电话?
详情:
如果我运行以下 cURL 命令,它会起作用:
curl -L --negotiate -u : -b ~/cookiejar.txt "https://myIdp.domain.net/oauth2/authorize?scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=my_client_id_here"
我不确定如何在 C# 代码中复制所有这些内容。
仅供参考:我在这个问题中专门询问了这个 cURL 命令(因为这个问题集中在模拟上):Replicate cURL Command Using Redirect and Cookies in .Net Core 3.1
【问题讨论】:
-
我尝试设置我的项目来重现您的问题。问题是我无法“模拟”您的身份服务器。您的身份服务器/身份验证端点是什么?根据我的经验,我使用
HttpClient而不是WebClient来执行请求,也许这可以解决您的问题(只是一个简短的猜测)? -
@Martin - 我希望我的问题不是针对我的身份服务器的。更多关于如何制作 cookie 来发送或其他内容。我会共享我的身份服务器端点,但不幸的是我的身份服务器位于防火墙后面。它是一个 WSO2 身份服务器。我已经用 HttpClient 进行了尝试,并得到了相同的结果。 (我展示了
WebClient的用法,因为我看到另一个问题说WebClient将包含凭据:stackoverflow.com/a/12675503/16241) -
只是一个盲目的猜测:) ...下一个问题:为什么
WindowsIdentity.RunImpersonated前面有一个await在documentation它只是void你没有使用默认WindowsIdentity来自System.Security.Principal命名空间? -
@Martin - 嗯,这很奇怪。 LinqPad 中的“悬停信息”显示它正在使用
System.Security.Principal。但是您发送的文档链接清楚地表明它只是无效的。不知道该怎么办... -
由于您无论如何都想使用当前身份,因此模拟可能不会有助于解决问题。如果您直接在
WebClient中配置凭据而不是依赖默认值,会有什么不同吗?
标签: c# .net-core .net-core-3.1