gpt4 book ai didi

c# - 在 .Net Core 3.1 中使用重定向和 Cookie 复制 cURL 命令

转载 作者:行者123 更新时间:2023-12-04 13:37:23 24 4
gpt4 key购买 nike

这似乎是一个远景。但是我已经看到几个答案表明在 .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选项在那里,它遵循重定向

  • 重定向以 401(未经授权)和 www-authenticate:Negotiate 响应。标题。
  • cURL 看到 www-authenticate:Negotiate header 并从操作系统获取 Kerberos token (因为 --negotiate-u 选项)。
  • cURL 调用带有 Authorization: Negotiate <kerberos token here> 附加 header 的重定向 url ( https://idp.domain.net/iwa-kerberos?state=state_guid_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 更多cookies . (由于 -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 跟随重定向到 https://localhost:5001/?code=code_guid_here&session_state=session_state_here 和添加的 cookie。
  • https://localhost:5001/?code=code_guid_here&session_state=session_state_here 的内容返回到 cURL 命令行。

  • 把这一切写出来,让它在 .Net 应用程序中工作似乎是一项严肃的任务。但我想我会问它是否内置在某个框架中。
    是否有一个 .Net Core Framework 类(或类似的)可以让我在 C# 代码中重现这个 cURL 命令?
    注意:我可以通过调用 powershell 来完成。这个问题是关于用 HttpClient 做的.

    最佳答案

    将 curl 标志转换为 HttpClient-LHttpClient应该自动跟随重定向,因为 HttpClientHandler.AllowAutoRedirect defaults to true .--negotiate -u :HttpClient如果您为其构造函数提供 HttpClientHandler 将处理协商为它提供凭据。由于您使用的是带有 -u : 的默认 Windows 凭据,您可以使用来自 this answer 的代码:

    var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
    -b ~/cookiejar.txt HttpClient可以通过为其构造函数提供 HttpClientHandler 来存储 cookie。与 CookieContainer :
    var client = new HttpClient(new HttpClientHandler() { CookieContainer = new CookieContainer() })
    这取决于 HttpClientHandler UseCookies = true , but it is true by default .
    返回内容
    您可以使用 HttpClient.GetAsync HttpResponseMessage.Content 阅读 HttpClient 的回复
    var response = await client.GetAsync("");
    var content = await response.Content.ReadAsStringAsync();
    一切结合
    这是构建的内容 HttpClient如果我们明确设置上面引用的每个值,那么发出等效请求应该看起来像:
    var client = new HttpClient(
    new HttpClientHandler()
    {
    // -L
    AllowAutoRedirect = true,

    // --negotiate -u :
    UseDefaultCredentials = true,

    // -b ~/cookiejar.txt
    CookieContainer = new CookieContainer(),
    UseCookies = true
    }
    );

    var response = await client.GetAsync("https://idp.domain.net/oauth2/authorize?scope=openid&response_type=code&redirect_uri=https://localhost:5001&client_id=client_id_here");
    var content = await response.Content.ReadAsStringAsync();

    关于c# - 在 .Net Core 3.1 中使用重定向和 Cookie 复制 cURL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60998324/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com