How to Encode and Decode Base64 string -Basic Authentication

In this article, we shall see how How to Encode and Decode Base64 string -Basic Authenticationin C# .NET or .NET core ecosystem.

We shall understand below aspects in the article,

So let’s get started.

Encode a String to Base64 string

Encode a string to Base 64 string using below logic,

            
        var byteArray = Encoding.ASCII.GetBytes($"{UserName}:{Password}");
        string encodeString = Convert.ToBase64String(byteArray);
          

What is Basic Authentication

Basic authentication is an Authentication Scheme built into the HTTP protocol which uses a simple username and password to access a restricted resource. HTTP Basic authentication is the simplest technique for enforcing restricted access to web resources.

Command

Authorization: Basic <credentials(base64)>

If you have UserName and Password is as “Test“, “Password” then Base64 string should be as below,

Authorization: Basic VGVzdDpQYXNzd29yZA===

Note:

Because base64 can easily be decoded, It’s recommended using Basic authentication using HTTPS/SSL only.

Basic Authentication- Encoded Header credentials

Encoding Basic Authentication credentials can be achieved using AuthenticationHeaderValue as below,

Let’s create an Authentication header for Basic authentication,

var clientAuthrizationHeader = new AuthenticationHeaderValue("Basic",
encodeString );

If you need to add Add Authorization header to the API request then you can use multiple approaches.

A few of them are listed below.

Request.Headers.Add("Authorization", clientAuthrizationHeader.ToString());

Or you can any other 2-3 techniques to add headers to request as discussed in the article: Adding a Custom Header to an ASP.NET Core Request.

Example:

Encode and Decode Base64 string

Decode Base64 string

Decoding a base64 string can be achieved using the below logic,

var encodedTextBytes = Convert.FromBase64String(encodeString );
string plainText = Encoding.UTF8.GetString(encodedTextBytes);

Basic Authentication- Decode Header credentials

Decoding Basic Authentication credentials can be achieved using AuthenticationHeaderValue as below,

     var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
     var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
     var credentials = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' }, 2);

Example:

Basic Authentication Base64 String encode and decode

References:

That’s all, Happy Coding!

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Summary

Today in this article we learned how to perform encoding of Basic Authentication credentials i.e UserName and Password. We also learned how to decode Basic Authentication credentials and read the credentials.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *