In this article, I am going to discuss how to implement the Role-Based Basic Authentication in ASP.NET Web API Applications. Please read our last article before proceeding to this article, where we discussed How to implement ASP.NET Web API Basic Authentication with an example. As part of this article, we are going to discuss the following pointers related to authentication and authorization.
- Why do we need Role-Based Authentication?
- How to Implement Role-Based Basic Authentication in Web API?
- Testing the Role-Based Basic Authentication using Postman.
- What are the advantages and disadvantages of using BASIC Authentication in Web API?
Why do we need Role-Based Authentication?
Let us understand this with an example. As shown in the below image, we have three resources i.e. GetAllMaleEmployees, GetAllFemaleEmployees, and GetAllEmployees in our service.
In our application, let say we have two types of Roles i.e. Admin and Superadmin and as per our business requirement,
- Only the users who have the Role Admin can access only to the GetAllMaleEmployees resource.
- The users who have the Role Superadmin can access only the GetAllFemaleEmployees resource.
- The GetAllEmployees resource can be accessed by both the Admin and Superadmin resource.
In order to achieve this, we need to implement Role-Based Authentication in ASP.NET Web API.
Implementing Role-Based Basic Authentication in Web API.
First, create an empty Web API application with the name RoleBasedBasicAuthenticationWEBAPI. Then Add the following User and Employee model to the Models folder
User.cs
Employee.cs
Now we need to add the UserBL and EmployeeBL class file within the Models folder.
UserBL.cs
EmployeeBL.cs
Now add one more class file with the name UserValidate and copy and paste the following code.
UserValidate.cs
Now create the BasicAuthenticationAttribute which will implement the AuthorizationFilterAttribute where we will put the logic for role-based basic authentication.
BasicAuthenticationAttribute.cs
Now we will create our custom Authorize Attribute which will inherit from AuthorizeAttribute where we will implement the logic to return an appropriate response when the Authorization failed.
MyAuthorizeAttribute.cs
Lets create a Web API 2 Empty Controller with the name EmployeeController and copy and paste the following code.
Thats it. We have done with our implementation.
Testing Role-Based Basic Authentication in Web API using Postman
If you are new to the postman, I strongly recommended you to read the following Video, where I discussed how to download and use postman to test rest services.
We need to pass the username and password in the Authorization header. The username and password need to be a colon (:) separated and must be in base64 encoded. To do so, just use the following website
Enter the username and password separated by a colon (:) in the “Encode to Base64 format†textbox, and then click on the “Encode†button as shown in the below diagram which will generate the Base64 encoded value. Let first generate the Base64 encoded string for the user AdminUser as shown in the below image
Once you generated the Base64 encoded string, lets see how to use basic authentication in the header to pass the Base64 encoded value. Here we need to use the Authorization header and the value will be the Base64 encoded string followed the “BASIC†as shown below.
Authorization: BASIC TWFsZVVzZXI6MTIzNDU2
The role Admin has been assigned to the AdminUser. So he can access only the following two resources
/api/AllMaleEmployees
/api/AllEmployees
But he cannot access the following resource
/api/AllFemaleEmployees
Let proofs this using the Postman.
/api/AllMaleEmployees
Here we got the response 200 OK.
/api/AllEmployees
Here we also got the response 200 OK as expected.
/api/AllFemaleEmployees
As you can see, here we got the response as 403 Forbidden which means the user is authenticated but not authorized to access the above resource. Similarly, you can test the other users.
Advantages and disadvantages of Basic Authentication in Web API.
Advantages:
- Internet standard.
- Supported by all major browsers.
- Relatively simple protocol.
Disadvantages:
- User credentials are sent in the request.
- Credentials are sent as plaintext.
- Credentials are sent with every request.
- No way to log out, except by ending the browser session.
- Vulnerable to cross-site request forgery (CSRF); requires anti-CSRF measures.
In the next Video, I am going to discuss how to consume the Web API from JavaScript and C# clients. Here, in this Video, I try to explain the Role-Based Basic Authentication in Web API Applications step by step with an example. I hope now you understood the need and use Role-Based Authentication in Web API.