Setting Authorization Header of HttpClient

前端 未结 21 2213
粉色の甜心
粉色の甜心 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

    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);
    

提交回复
热议问题