Setting Authorization Header of HttpClient

前端 未结 21 1887
粉色の甜心
粉色の甜心 2020-11-22 14:53

I have an HttpClient that I am using for a REST API. However I am having trouble setting up the Authorization header. I need to set the header to the token I received from d

相关标签:
21条回答
  • 2020-11-22 15:20

    I was setting the bearer token

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

    It was working in one endpoint, but not another. The issue was that I had lower case b on "bearer". After change now it works for both api's I'm hitting. Such an easy thing to miss if you aren't even considering it as one of the haystacks to look in for the needle.

    Make sure to have "Bearer" - with capital.

    0 讨论(0)
  • 2020-11-22 15:20

    It may be easier to use an existing library.

    For example, the extension methods below are added with 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);
    
    0 讨论(0)
  • 2020-11-22 15:20

    In net .core you can use

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

    or

    var client = new HttpClient();
    client.SetBearerToken(token);
    
    0 讨论(0)
  • 2020-11-22 15:23

    So the way to do it is the following,

    httpClient.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer", "Your Oauth token");
    
    0 讨论(0)
  • 2020-11-22 15:25

    This may help Setting the header:

    WebClient client = new WebClient();
    
    string authInfo = this.credentials.UserName + ":" + this.credentials.Password;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    client.Headers["Authorization"] = "Basic " + authInfo;
    
    0 讨论(0)
  • 2020-11-22 15:26
    request.DefaultRequestHeaders.Authorization = 
        new AuthenticationHeaderValue(
            "Basic", Convert.ToBase64String(
                System.Text.ASCIIEncoding.ASCII.GetBytes(
                   $"{yourusername}:{yourpwd}")));
    
    0 讨论(0)
提交回复
热议问题