Vac*_*ano 6 c# .net-core .net-core-3.1
我有以下代码:
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);
}
});
Run Code Online (Sandbox Code Playgroud)
它基本上构造一个 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"
Run Code Online (Sandbox Code Playgroud)
我不确定如何在 C# 代码中复制所有这些。
仅供参考:我在这个问题中专门询问了这个 cURL 命令(因为这个问题的重点是模拟):Replicate cURL Command Using Redirect and Cookies in .Net Core 3.1
我面前没有 Windows 盒子,所以我无法彻底验证这一点。但这似乎有点像一个蛇坑,基于这里的讨论(特别是从下面的评论): https ://github.com/dotnet/runtime/issues/24009#issuecomment-544511572
关于如何在异步调用中保持身份似乎有多种意见。
但如果你看一下评论中的例子,
app.Use(async (context, next) =>
{
await WindowsIdentity.RunImpersonated(someToken, () => next());
});
Run Code Online (Sandbox Code Playgroud)
它看起来不像您作为第二个参数发送的函数WindowsIdentity.RunImpersonated应该是异步的。
你有没有尝试过:
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, () =>
{
using (var client = new WebClient{ UseDefaultCredentials = true })
{
var response = client.DownloadString(combinedUrl);
Console.WriteLine(response);
}
});
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到 Microsoft 文档WindowsIdentity.RunImpersonated:https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated ?view=netcore-3.1
| 归档时间: |
|
| 查看次数: |
5666 次 |
| 最近记录: |