What is the best way for HttpClient's Authorization?

Jassim Al Rahma 1,616 Reputation points
2023-01-11T20:20:23.0333333+00:00

Hi,

What is the best and more secure way for HttpClient's Authorization?

I want to use a UUID for authorizing the Http.

Thanks,

Jassim

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,326 Reputation points
    2023-01-11T21:22:42.7766667+00:00

    HttpClient is just a wrapper around an HTTP request. Authentication in HTTP requests generally involves sending a POST request to an auth endpoint and getting back some data. That data is then attached to subsequent HTTP requests, generally as an HTTP header. There are already well-defined authentication systems defined for HTTP that you should use. Please do not invent your own.

    For basic authentication you attach the data as part of the HTTP request's Authorization header with a type of Basic. For C# that can be done by attaching a BasicAuthenticationHeaderValue with the username and password specified as parameters. That header then encodes everything.

    For an API key-style authentication where a specific key must be passed in the header you would just add the HTTP header to the request along with the value.

    There are libraries already provided that can do this for you automatically but it depends on what version of .NET you're using. I would recommend that you look at the version you're using to see if there are already extensions for it.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.