When interacting with REST APIs, if the API requires authentication, you'll often need to include credentials in your request. Basic Authentication is one of the simplest ways to do this. This blog provides a code example of how to use Basic Authentication in C# using the popular RestSharp library.
Basic Authentication With RestSharp in C#
Getting Started
When working with REST APIs, authentication is a crucial part of securing access to endpoints. One of the simplest forms of authentication is Basic Authentication, where the client sends a base64-encoded username and password with each request. While it's not the most secure method on its own (especially without HTTPS), it’s widely supported and often used for quick integrations or internal APIs.
What is Basic Authentication?
Basic Authentication sends the username and password encoded in Base64 as part of the Authorization header in the HTTP request. The format is:
Authorization: Basic <base64(username:password)>
For example, for a username admin and password 1234, the header would look like:
Authorization: Basic YWRtaW46MTIzNA==
Note: Never use Basic Auth over an unencrypted connection—always use HTTPS.
Example: HTTP Basic Authentication with RestSharp
The Below two examples send requests to an API by manually encoding the header and using the built-in authenticator. The requests are in JSON format, and a Basic Authentication value is included in the header.
Basic Authentication with Manually Encoding
Here is a simple C# example that demonstrates how to make a request with Basic Authentication using RestSharp:
IRestRequest request = new RestRequest(Method.GET);
// Encode the credentials
var username = "your_username";
var password = "your_password";
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
request.AddHeader("content-type", "application/json");
request.AddHeader("content-Length","0");
request.AddHeader("Authorization", "Basic " + credentials);
request.RequestFormat = DataFormat.Json;
request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);
RestClient client = new RestClient("url");
IRestResponse response = client.Execute(request);
string responsevalue = response.Content;
Basic Authentication Using Built-in Authentication
As of RestSharp version 107, you can also use RestClientOptions to handle basic auth more cleanly:
var options = new RestClientOptions("https://api.example.com")
{
Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("your_username", "your_password")
};
IRestRequest request = new RestRequest("/your-endpoint",Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("content-Length","0");
request.RequestFormat = DataFormat.Json;
request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);
RestClient client = new RestClient(options);
IRestResponse response = client.Execute(request);
string responsevalue = response.Content;
Summary
Basic Authentication is straightforward to implement with RestSharp, especially with the newer versions that support built-in authenticators. While it's not the most secure form of authentication, it can be suitable for quick prototypes or internal APIs when paired with HTTPS. Make sure to follow security best practices, and consider more secure methods for public or sensitive applications.
Thanks