【问题标题】:Setting Authorization Header of HttpClient设置HttpClient的授权头
【发布时间】:2025-11-22 19:20:03
【问题描述】:

我有一个用于 REST API 的 HttpClient。但是我在设置授权标头时遇到问题。我需要将标头设置为我从执行 OAuth 请求中收到的令牌。 我看到了一些 .NET 的代码,建议如下,

httpClient.DefaultRequestHeaders.Authorization = new Credential(OAuth.token);

但是,WinRT 中不存在 Credential 类。有人知道如何设置授权标头吗?

【问题讨论】:

  • Credential 类属于哪个命名空间?
  • @kampsj 我不知道,因为它是 WinRT 中不存在的 .NET 命名空间
  • 为什么不 request.Headers.Add("Authorization", token);
  • @ahll 比原来的日期晚了几年,但是现在这会给出一个 InvalidOperationException 并带有“Headers misused”消息。
  • @NeilMonroe 格式?授权:不记名

标签: c# rest oauth windows-runtime


【解决方案1】:

所以方法如下,

httpClient.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", "Your Oauth token");

【讨论】:

  • 如何获得“您的 Oauth 令牌”?
  • 我使用的是:client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "encrypted user/pwd"); 从 Advanced Rest Client chrome 扩展中获取加密的用户/密码。
  • @Red fyi,第二个参数是 base64 编码的用户:密码(未加密)。
  • 我的应用程序很高兴地使用它很长时间了,然后突然我开始收到 RuntimeBinderException。我不得不切换到 httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer", "Your Oauth token"); 让它再次运行。
  • @kraeg,您列出的代码无法编译,您是不是要像这样连接最后两个字符串:client.DefaultRequestHeaders.Add("Authorization", "Bearer" + "Your Oauth token ");
【解决方案2】:
request.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
               $"{yourusername}:{yourpwd}")));

【讨论】:

  • 这是行不通的,如果您检查授权标头仅包含字符串 Basic 之外的任何内容。
  • 谁能解释为什么将用户名和密码转换为 base64 字符串很重要?它没有提供真正的加密,那这有什么关系呢?
  • @JonathanWood 因为这就是它被定义为使用的方式。 Basic 不提供加密,仅提供足够的编码以避免在标题中选择密码字符时出现问题。
  • 您在这里使用 ASCII 编码有什么特别的原因吗?我认为使用 UTF8 编码没有问题,因为无论如何我们都是 Base64 编码的。我想我想知道基本身份验证规范是否说用户名:密码组合应该只使用 ASCII?
  • System.Text.ASCIIEncoding.ASCII其实是在父类Encoding中。所以你可以改用 System.Text.Encoding.ASCII。
【解决方案3】:

我正在寻找一种解决此问题的好方法,并且我正在研究同样的问题。希望这个答案能帮助像我一样遇到同样问题的每个人。

using (var client = new HttpClient())
{
    var url = "https://www.theidentityhub.com/{tenant}/api/identity/v1";
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
    var response = await client.GetStringAsync(url);
    // Parse JSON response.
    ....
}

来自https://www.theidentityhub.com/hub/Documentation/CallTheIdentityHubApi的参考

【讨论】:

  • 我正在做同样的事情@willie,我仍然从我的 API 获得 401
  • 嗨@SomethingOn我认为你没有得到正确的令牌密钥,所以你得到了401,我会在我的个人“”上分享我的方式,希望它可以帮助你处理你的问题.PS 稍等
  • 您不应该将 HttpClient 放在 using 块中。 (是的,我知道这听起来倒退了,但是如果你使用using 而不是仅仅回收 HttpClient,你会泄漏连接。)
  • @JonathanAllen 如果您指的是here 描述的连接泄漏,可能值得注意的是,很少有读者在文章 cmets 中提到泄漏可能与如何处理响应有关,因为许多开发人员忘记处置 HttpResponse 本身,只处置 HttpClient
【解决方案4】:

由于重用 HttpClient 实例是一种性能和端口耗尽问题的好习惯,并且因为没有一个答案给出这个解决方案(甚至引导你对于不良做法:(),我在这里放了一个链接,指向我在类似问题上所做的答案:

https://*.com/a/40707446/717372

关于如何正确使用 HttpClient 的一些资料:

【讨论】:

  • 端口耗尽问题可不是开玩笑的。它几乎不会在 QA 中发生,但会影响到任何在生产中大量使用的项目。
  • 查看我的帖子以获取具体示例*.com/a/59052193/790635
【解决方案5】:

我建议你:

HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer <token>");

然后你可以这样使用它:

var response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
    responseMessage = await response.Content.ReadAsAsync<ResponseMessage>();
}

【讨论】:

  • 例如,如果您的令牌每 1 小时超时一次,那么您必须使用此解决方案更新 HttpClient。我建议检查您的令牌是否仍然有效,否则刷新它并将其添加到 HttpRequestMessage
【解决方案6】:

如果您想使用 Bearer Token 发送HttpClient 请求,此代码可能是一个很好的解决方案:

var requestMessage = new HttpRequestMessage
{
    Method = HttpMethod.Post,
    Content = new StringContent(".....", Encoding.UTF8, "application/json"),
    RequestUri = new Uri(".....")
};

requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Your token");

var response = await _httpClient.SendAsync(requestMessage);

【讨论】:

    【解决方案7】:

    我正在设置不记名令牌

    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    

    它在一个端点上工作,但不是在另一个端点上。问题是我在"bearer" 上有小写b。现在更改后,它适用于我正在打的两个 api。如果您甚至不将其视为寻找针头的干草堆之一,那么这很容易错过。

    确保"Bearer" - 大写。

    【讨论】:

      【解决方案8】:

      使用基本授权和 Json 参数。

      using (HttpClient client = new HttpClient())
      {
          var request_json = "your json string";
      
          var content = new StringContent(request_json, Encoding.UTF8, "application/json");
      
          var authenticationBytes = Encoding.ASCII.GetBytes("YourUsername:YourPassword");
      
          client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                  Convert.ToBase64String(authenticationBytes));
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      
          var result = await client.PostAsync("YourURL", content);
      
          var result_string = await result.Content.ReadAsStringAsync();
      }
      

      【讨论】:

      • 您不应在这样的示例中包含禁用 SSL 证书检查的代码。人们可能会盲目地复制您的代码而没有意识到它的作用。我已经为你删除了这些行。
      【解决方案9】:

      使用 C# HttpClient 设置基本身份验证。以下代码对我有用。

         using (var client = new HttpClient())
              {
                  var webUrl ="http://localhost/saleapi/api/";
                  var uri = "api/sales";
                  client.BaseAddress = new Uri(webUrl);
                  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                  client.DefaultRequestHeaders.ConnectionClose = true;
      
                  //Set Basic Auth
                  var user = "username";
                  var password = "password";
                  var base64String =Convert.ToBase64String( Encoding.ASCII.GetBytes($"{user}:{password}"));
                  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",base64String);
      
                  var result = await client.PostAsJsonAsync(uri, model);
                  return result;
              }
      

      【讨论】:

      • 代码中的“模型”是什么?
      【解决方案10】:

      如果您想重复使用HttpClient,建议不要使用DefaultRequestHeaders,因为它们用于与每个请求一起发送。

      你可以试试这个:

      var requestMessage = new HttpRequestMessage
          {
              Method = HttpMethod.Post,
              Content = new StringContent("...", Encoding.UTF8, "application/json"),
              RequestUri = new Uri("...")
          };
      
      requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", 
          Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{user}:{password}")));
      
      var response = await _httpClient.SendAsync(requestMessage);
      

      【讨论】:

      • 是的,很难找到这个答案。我猜很多人没有过多阅读文档,因为最佳实践是让 HttpClient 成为静态成员变量以避免端口耗尽问题。然后使用 DefaultRequestHeaders 也没什么意义,尤其是,如果我们在这里谈论令牌/承载身份验证,因为这些令牌将不可避免地过期!因此,基于此的默认设置只是倒退。
      【解决方案11】:

      这就是我的做法:

      using (HttpClient httpClient = new HttpClient())
      {
         Dictionary<string, string> tokenDetails = null;
         var messageDetails = new Message { Id = 4, Message1 = des };
         HttpClient client = new HttpClient();
         client.BaseAddress = new Uri("http://localhost:3774/");
         var login = new Dictionary<string, string>
             {
                 {"grant_type", "password"},
                 {"username", "sa@role.com"},
                 {"password", "lopzwsx@23"},
             };
         var response = client.PostAsync("Token", new FormUrlEncodedContent(login)).Result;
         if (response.IsSuccessStatusCode)
         {
            tokenDetails = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
            if (tokenDetails != null && tokenDetails.Any())
            {
               var tokenNo = tokenDetails.FirstOrDefault().Value;
               client.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenNo);
               client.PostAsJsonAsync("api/menu", messageDetails)
                   .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
            }
         }
      }
      

      这个 youtube 视频对我帮助很大。请检查一下。 https://www.youtube.com/watch?v=qCwnU06NV5Q

      【讨论】:

        【解决方案12】:

        6 年后,但添加这个以防它帮助某人。

        https://www.codeproject.com/Tips/996401/Authenticate-WebAPIs-with-Basic-and-Windows-Authen

        var authenticationBytes = Encoding.ASCII.GetBytes("<username>:<password>");
        using (HttpClient confClient = new HttpClient())
        {
          confClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", 
                 Convert.ToBase64String(authenticationBytes));
          confClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.MediaType));  
          HttpResponseMessage message = confClient.GetAsync("<service URI>").Result;
          if (message.IsSuccessStatusCode)
          {
            var inter = message.Content.ReadAsStringAsync();
            List<string> result = JsonConvert.DeserializeObject<List<string>>(inter.Result);
          }
        }
        

        【讨论】:

          【解决方案13】:

          对于现在(2021 年)发现这个旧线程的任何人,请查看 this documentation 关于 HttpClientFactory 的信息,它是可注入的,并且还将在每个请求上重新运行,避免过期的令牌,这将使它对不记名令牌、生成的客户端有用,池化等。

          TL;DR:使用 HttpClientFactoryDelegatingHandler 将作为中间件处理您配置的客户端的所有传出请求。

          这就是我为 Azure Identity 添加我的承载(由 Azure 管理)的方式,但你当然可以随心所欲地获取令牌;

          using Microsoft.Azure.Services.AppAuthentication;
          using System.Net.Http;
          using System.Threading;
          using System.Threading.Tasks;
          
          public class BearerTokenHandler : DelegatingHandler
              {
                  public BearerTokenHandler(AzureServiceTokenProvider tokenProvider, string resource)
                  {
                      TokenProvider = tokenProvider;
                      Resource = resource;
                  }
          
                  public AzureServiceTokenProvider TokenProvider { get; }
                  public string Resource { get; }
          
                  protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
                  {
                      if (!request.Headers.Contains("Authorization"))
                      {
                          // Fetch your token here
                          string token = await TokenProvider.GetAccessTokenAsync(Resource);
                          request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                      }
          
                      return await base.SendAsync(request, cancellationToken);
                  }
              }
          

          我在 Startup 中像这样配置我的类型化客户端(使用 NSwag 生成);

             var accessTokenProvider = new AzureServiceTokenProvider("<your-connection-string-for-access-token-provider>");
          
            builder.Services.AddHttpClient<IOrdersClient, OrdersClient>().ConfigureHttpClient(async conf =>
                      {
                          conf.BaseAddress = new Uri("<your-api-base-url>");
                      }).AddHttpMessageHandler(() => new BearerTokenHandler(accessTokenProvider, "https://your-azure-tenant.onmicrosoft.com/api"));
          

          然后你可以在任何你喜欢的地方注入你的 IOrdersClient 并且所有的请求都会有承载。

          【讨论】:

            【解决方案14】:

            UTF8 选项

            request.DefaultRequestHeaders.Authorization = 
            new AuthenticationHeaderValue(
                "Basic", Convert.ToBase64String(
                    System.Text.Encoding.UTF8.GetBytes(
                       $"{yourusername}:{yourpwd}")));
            

            【讨论】:

              【解决方案15】:

              使用AuthenticationHeaderValue 类的System.Net.Http 程序集

              public AuthenticationHeaderValue(
                  string scheme,
                  string parameter
              )
              

              我们可以为httpclient 设置或更新现有的Authorization 标头,如下所示:

              httpclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", TokenResponse.AccessToken);
              

              【讨论】:

                【解决方案16】:

                BaseWebApi.cs

                public abstract class BaseWebApi
                {
                    //Inject HttpClient from Ninject
                    private readonly HttpClient _httpClient;
                    public BaseWebApi(HttpClient httpclient)
                    {
                        _httpClient = httpClient;
                    }
                
                    public async Task<TOut> PostAsync<TOut>(string method, object param, Dictionary<string, string> headers, HttpMethod httpMethod)
                    {
                        //Set url
                
                        HttpResponseMessage response;
                        using (var request = new HttpRequestMessage(httpMethod, url))
                        {
                            AddBody(param, request);
                            AddHeaders(request, headers);
                            response = await _httpClient.SendAsync(request, cancellationToken);
                        }
                
                        if(response.IsSuccessStatusCode)
                        {
                             return await response.Content.ReadAsAsync<TOut>();
                        }
                        //Exception handling
                    }
                
                    private void AddHeaders(HttpRequestMessage request, Dictionary<string, string> headers)
                    {
                        request.Headers.Accept.Clear();
                        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                
                        if (headers == null) return;
                
                        foreach (var header in headers)
                        {
                            request.Headers.Add(header.Key, header.Value);
                        }
                    }
                
                    private static void AddBody(object param, HttpRequestMessage request)
                    {
                        if (param != null)
                        {
                            var content = JsonConvert.SerializeObject(param);
                            request.Content = new StringContent(content);
                            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        }
                    }
                

                SubWebApi.cs

                public sealed class SubWebApi : BaseWebApi
                {
                    public SubWebApi(HttpClient httpClient) : base(httpClient) {}
                
                    public async Task<StuffResponse> GetStuffAsync(int cvr)
                    {
                        var method = "get/stuff";
                        var request = new StuffRequest 
                        {
                            query = "GiveMeStuff"
                        }
                        return await PostAsync<StuffResponse>(method, request, GetHeaders(), HttpMethod.Post);
                    }
                    private Dictionary<string, string> GetHeaders()
                    {
                        var headers = new Dictionary<string, string>();
                        var basicAuth = GetBasicAuth();
                        headers.Add("Authorization", basicAuth);
                        return headers;
                    }
                
                    private string GetBasicAuth()
                    {
                        var byteArray = Encoding.ASCII.GetBytes($"{SystemSettings.Username}:{SystemSettings.Password}");
                        var authString = Convert.ToBase64String(byteArray);
                        return $"Basic {authString}";
                    }
                }
                

                【讨论】:

                  【解决方案17】:

                  使用现有库可能更容易。

                  例如,下面的扩展方法是随 Identity Server 4 添加的 https://www.nuget.org/packages/IdentityModel/

                   public static void SetBasicAuthentication(this HttpClient client, string userName, string password);
                      //
                      // Summary:
                      //     Sets a basic authentication header.
                      //
                      // Parameters:
                      //   request:
                      //     The HTTP request message.
                      //
                      //   userName:
                      //     Name of the user.
                      //
                      //   password:
                      //     The password.
                      public static void SetBasicAuthentication(this HttpRequestMessage request, string userName, string password);
                      //
                      // Summary:
                      //     Sets a basic authentication header for RFC6749 client authentication.
                      //
                      // Parameters:
                      //   client:
                      //     The client.
                      //
                      //   userName:
                      //     Name of the user.
                      //
                      //   password:
                      //     The password.
                      public static void SetBasicAuthenticationOAuth(this HttpClient client, string userName, string password);
                      //
                      // Summary:
                      //     Sets a basic authentication header for RFC6749 client authentication.
                      //
                      // Parameters:
                      //   request:
                      //     The HTTP request message.
                      //
                      //   userName:
                      //     Name of the user.
                      //
                      //   password:
                      //     The password.
                      public static void SetBasicAuthenticationOAuth(this HttpRequestMessage request, string userName, string password);
                      //
                      // Summary:
                      //     Sets an authorization header with a bearer token.
                      //
                      // Parameters:
                      //   client:
                      //     The client.
                      //
                      //   token:
                      //     The token.
                      public static void SetBearerToken(this HttpClient client, string token);
                      //
                      // Summary:
                      //     Sets an authorization header with a bearer token.
                      //
                      // Parameters:
                      //   request:
                      //     The HTTP request message.
                      //
                      //   token:
                      //     The token.
                      public static void SetBearerToken(this HttpRequestMessage request, string token);
                      //
                      // Summary:
                      //     Sets an authorization header with a given scheme and value.
                      //
                      // Parameters:
                      //   client:
                      //     The client.
                      //
                      //   scheme:
                      //     The scheme.
                      //
                      //   token:
                      //     The token.
                      public static void SetToken(this HttpClient client, string scheme, string token);
                      //
                      // Summary:
                      //     Sets an authorization header with a given scheme and value.
                      //
                      // Parameters:
                      //   request:
                      //     The HTTP request message.
                      //
                      //   scheme:
                      //     The scheme.
                      //
                      //   token:
                      //     The token.
                      public static void SetToken(this HttpRequestMessage request, string scheme, string token);
                  

                  【讨论】:

                    【解决方案18】:

                    这可能有效,如果您从服务接收 json 或 xml,并且我认为这可以让您了解标头和 T 类型的工作原理,如果您使用函数 MakeXmlRequest(将结果放入 xmldocumnet ) 和 MakeJsonRequest(将 json 放入您希望与 json 响应具有相同结构的类中) 下一种方式

                    /*-------------------------example of use-------------*/
                    MakeXmlRequest<XmlDocument>("your_uri",result=>your_xmlDocument_variable =     result,error=>your_exception_Var = error);
                    
                    MakeJsonRequest<classwhateveryouwant>("your_uri",result=>your_classwhateveryouwant_variable=result,error=>your_exception_Var=error)
                    /*-------------------------------------------------------------------------------*/
                    
                    
                    public class RestService
                    {
                        public void MakeXmlRequest<T>(string uri, Action<XmlDocument> successAction, Action<Exception> errorAction)
                        {
                            XmlDocument XMLResponse = new XmlDocument();
                            string wufooAPIKey = ""; /*or username as well*/
                            string password = "";
                            StringBuilder url = new StringBuilder();
                            url.Append(uri);
                            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url.ToString());
                            string authInfo = wufooAPIKey + ":" + password;
                            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                            request.Timeout = 30000;
                            request.KeepAlive = false;
                            request.Headers["Authorization"] = "Basic " + authInfo;
                            string documento = "";
                            MakeRequest(request,response=> documento = response,
                                                (error) =>
                                                {
                                                 if (errorAction != null)
                                                 {
                                                    errorAction(error);
                                                 }
                                                }
                                       );
                            XMLResponse.LoadXml(documento);
                            successAction(XMLResponse);
                        }
                    
                    
                    
                        public void MakeJsonRequest<T>(string uri, Action<T> successAction, Action<Exception> errorAction)
                        {
                            string wufooAPIKey = "";
                            string password = "";
                            StringBuilder url = new StringBuilder();
                            url.Append(uri);
                            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url.ToString());
                            string authInfo = wufooAPIKey + ":" + password;
                            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                            request.Timeout = 30000;
                            request.KeepAlive = false;
                            request.Headers["Authorization"] = "Basic " + authInfo;
                           // request.Accept = "application/json";
                          //  request.Method = "GET";
                            MakeRequest(
                               request,
                               (response) =>
                               {
                                   if (successAction != null)
                                   {
                                       T toReturn;
                                       try
                                       {
                                           toReturn = Deserialize<T>(response);
                                       }
                                       catch (Exception ex)
                                       {
                                           errorAction(ex);
                                           return;
                                       }
                                       successAction(toReturn);
                                   }
                               },
                               (error) =>
                               {
                                   if (errorAction != null)
                                   {
                                       errorAction(error);
                                   }
                               }
                            );
                        }
                        private void MakeRequest(HttpWebRequest request, Action<string> successAction, Action<Exception> errorAction)
                        {
                            try{
                                using (var webResponse = (HttpWebResponse)request.GetResponse())
                                {
                                    using (var reader = new StreamReader(webResponse.GetResponseStream()))
                                    {
                                        var objText = reader.ReadToEnd();
                                        successAction(objText);
                                    }
                                }
                            }catch(HttpException ex){
                                errorAction(ex);
                            }
                        }
                        private T Deserialize<T>(string responseBody)
                        {
                            try
                            {
                                var toReturns = JsonConvert.DeserializeObject<T>(responseBody);
                                 return toReturns;
                            }
                            catch (Exception ex)
                            {
                                string errores;
                                errores = ex.Message;
                            }
                            var toReturn = JsonConvert.DeserializeObject<T>(responseBody);
                            return toReturn;
                        }
                    }
                    }
                    

                    【讨论】:

                      【解决方案19】:

                      Oauth 流程很复杂,总是存在一个或另一个错误的空间。 我的建议是始终使用样板代码和一组库进行 OAuth 身份验证流程。这将使您的生活更轻松。

                      这是一组库的链接。OAuth Libraries for .Net

                      【讨论】:

                        【解决方案20】:

                        首先,我不会直接使用HttpClient。犯错太容易了——尤其是在头球方面。 DefaultHeadersCollection 不是不可变的,也不是线程安全的,因为应用程序的其他部分可以更改您的标题。最好在拨打电话时设置标题。如果您正在使用抽象,并且建议这样做,因为该区域中的类有点混乱,您可能希望有一个标题集合,并在发送之前将它们放在您的 HttpRequestMessage 上。您需要确保将内容标题放在内容上,而不是消息上。

                        Code Reference

                        foreach (var headerName in request.Headers.Names)
                        {
                            //"Content-Type"
                            if (string.Compare(headerName, HeadersExtensions.ContentTypeHeaderName, StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                //Note: not sure why this is necessary...
                                //The HttpClient class seems to differentiate between content headers and request message headers, but this distinction doesn't exist in the real world...
                                //TODO: Other Content headers
                                httpContent?.Headers.Add(HeadersExtensions.ContentTypeHeaderName, request.Headers[headerName]);
                            }
                            else
                            {
                                httpRequestMessage.Headers.Add(headerName, request.Headers[headerName]);
                            }
                        }
                        

                        这是一个可用于发送包含标头的请求的数据结构。

                        Code Reference

                        public interface IRequest
                        {
                            CancellationToken CancellationToken { get; }
                            string? CustomHttpRequestMethod { get; }
                            IHeadersCollection Headers { get; }
                            HttpRequestMethod HttpRequestMethod { get; }
                            AbsoluteUrl Uri { get; }
                        }
                        
                        public interface IRequest<TBody> : IRequest
                        {
                            TBody? BodyData { get; }
                        }
                        

                        还有一个标题集合:

                        Code Reference

                        public sealed class HeadersCollection : IHeadersCollection
                        {
                            #region Fields
                            private readonly IDictionary<string, IEnumerable<string>> dictionary;
                            #endregion
                        
                            #region Public Constructors
                        
                            public HeadersCollection(IDictionary<string, IEnumerable<string>> dictionary) => this.dictionary = dictionary;
                        
                            public HeadersCollection(string key, string value) : this(ImmutableDictionary.CreateRange(
                                        new List<KeyValuePair<string, IEnumerable<string>>>
                                        {
                                            new(key, ImmutableList.Create(value))
                                        }
                                        ))
                            {
                            }
                        
                            #endregion Public Constructors
                        
                            #region Public Properties
                            public static HeadersCollection Empty { get; } = new HeadersCollection(ImmutableDictionary.Create<string, IEnumerable<string>>());
                            public IEnumerable<string> Names => dictionary.Keys;
                            IEnumerable<string> IHeadersCollection.this[string name] => dictionary[name];
                            #endregion Public Properties
                        
                            #region Public Methods
                            public bool Contains(string name) => dictionary.ContainsKey(name);
                        
                            public IEnumerator<KeyValuePair<string, IEnumerable<string>>> GetEnumerator() => dictionary.GetEnumerator();
                        
                            IEnumerator IEnumerable.GetEnumerator() => dictionary.GetEnumerator();
                            public override string ToString() => string.Join("\r\n", dictionary.Select(kvp => $"{kvp.Key}: {string.Join(", ", kvp.Value)}\r\n"));
                            #endregion
                        }
                        

                        查看所有工作代码和示例here

                        【讨论】:

                          【解决方案21】:

                          如果您使用 Visual Studio IISExpress 调试模式并连接到 HTTP 端口而不是 HTTPS 端口,您可能会发现身份验证标头正在被丢弃。

                          切换到 SLL 连接,它们会再次出现。

                          不确定原因,可能是设置重定向了 http 流量并导致身份验证被删除。

                          【讨论】:

                            【解决方案22】:
                            static async Task<AccessToken> GetToken()
                            {
                                    string clientId = "XXX";
                                    string clientSecret = "YYY";
                                    string credentials = String.Format("{0}:{1}", clientId, clientSecret);
                            
                                    using (var client = new HttpClient())
                                    {
                                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
                                        List<KeyValuePair<string, string>> requestData = new List<KeyValuePair<string, string>>();
                                        requestData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
                                        FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);
                                        var request = await client.PostAsync("https://accounts.spotify.com/api/token", requestBody);
                                        var response = await request.Content.ReadAsStringAsync();
                                        return JsonConvert.DeserializeObject<AccessToken>(response);
                                    }
                                }
                            

                            【讨论】:

                            • 欢迎来到*。除了您提供的答案之外,请考虑提供一个简短说明,说明为什么以及如何解决此问题。
                            【解决方案23】:

                            在 net .core 中你可以使用

                            var client = new HttpClient();
                            client.SetBasicAuthentication(userName, password);
                            

                            var client = new HttpClient();
                            client.SetBearerToken(token);
                            

                            【讨论】:

                            • 第一个示例不起作用,因为SetBasicAuthentication() 默认情况下不可用,因此它必须是扩展方法。它是在哪里定义的?
                            【解决方案24】:

                            这可能有助于设置标题:

                            WebClient client = new WebClient();
                            
                            string authInfo = this.credentials.UserName + ":" + this.credentials.Password;
                            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                            client.Headers["Authorization"] = "Basic " + authInfo;
                            

                            【讨论】:

                            最近更新 更多