【发布时间】:2020-07-14 19:44:20
【问题描述】:
这似乎是一个长远的目标。但是我已经看到了几个答案,表明在 .Net Core 应用程序中需要 cURL 时应该使用 HttpClient(和类似的)。
我有以下 cURL 命令(完美运行):
curl -v -L --negotiate -u : -b ~/cookiejar.txt "https://idp.domain.net/oauth2/authorize?scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=client_id_here"
这个命令的流程是这样的:
- 加载提供的 url (https://idp.domain.net/oauth2/authorize....)
- 获取 302 响应以重定向到 https://idp.domain.net/iwa-kerberos?state=state_guid_here
- 因为有
-L选项,所以它遵循重定向
- 因为有
- 重定向响应带有
www-authenticate:Negotiate标头的 401(未授权)。 - cURL 看到
www-authenticate:Negotiate标头并从操作系统获取 Kerberos 令牌(由于--negotiate和-u选项)。 - cURL 调用重定向 url (https://idp.domain.net/iwa-kerberos?state=state_guid_here) 并附加标头
Authorization: Negotiate <kerberos token here>。 - 返回的 302 响应重定向到 https://idp.domain.net/commonauth?state=state_guid_here&iwaauth=1 并带有添加的 cookie
- 由于
-b选项,cookie 被 cURL 拾取。
- 由于
- cURL 调用重定向 url (https://idp.domain.net/commonauth?state=state_guid_here&iwaauth=1)使用上一步的 302 返回的 cookie。
- 返回另一个 302 重定向。重定向到 https://idp.domain.net/oauth2/authorize?sessionDataKey=session_key_guid_here带有更多 cookie。 (由于
-b选项再次被选中。) - 重定向到https://idp.domain.net/oauth2/authorize?sessionDataKey=session_key_guid_here 之后添加了cookie。
- 另一个 302 重定向返回到 https://localhost:5001/?code=code_guid_here&session_state=session_state_here(添加了 cookie)。
- cURL 使用添加的 cookie 跟随重定向到 https://localhost:5001/?code=code_guid_here&session_state=session_state_here。
- https://localhost:5001/?code=code_guid_here&session_state=session_state_here 的内容返回到 cURL 命令行。
把这一切写出来,让它在 .Net 应用程序中工作似乎是一项艰巨的任务。但我想我会问它是否内置在某个地方的框架中。
是否有 .Net Core Framework 类(或类似的)可以让我在 C# 代码中重现此 cURL 命令?
注意:我可以通过调用 powershell 来做到这一点。这个问题是关于使用HttpClient。
【问题讨论】:
-
如果你尝试只使用
HttpClient(和UseDefaultCredentials = true),那么它会在哪一步失败? -
@Evk - 将
UseDefaultCredentials和AllowAutoRedirect都设置为true,它会进入第3 步并停止。
标签: c# curl .net-core .net-core-3.1