Azure AD Authentication on Blazor WebAssembly


Implementing Azure AD Authentication In Blazor web assembly is very much similar to Blazor Server.

Please read the below post to know more details about azure portal configuration for Blazor Server

Step 1: Register the single page application on azure portal like this
Note: donot forget to append localhost URL with authentication/login-callback

Step 2: Select the Access Token and Id Token like this

Now create the Blazor Webassembly project with Microsoft Identity Platform

Now it will install, all the Authentication related nuget package and boilerplate code for us.

Step 3: Go to appsettings.json file and keep the TenantId and ClientId here.

Step 4: Now run the application, It will work as expected.
Advertisement

Azure AD authentication on Blazor Server


Recently I have used Azure AD(Active Directive) authentication on my Blazor project, So I am writing small post on this topic.

As you know Azure Active Directory (Azure AD) is a cloud-based identity and access management service. It is very simple and easy to integrate in Blazor Application.

Step1: Create the Blazor server application like this

Step2: Select the Authentication Type as “Microsoft Identity Platform”

Step 3: Now go to Azure portal and click on Active Directory

Step4: Click on Add and App registration link then you will see the page like this

Step 5: Click on Register and pick the client and tenant Id

Step 6: Go to Authentication then select the Id taken and Access Token and save it, like this image

Step 7: Go to appsettings.json file and file the required input like this

Note: you will get the Domain Name from here

Now run the application, It will work.

How to Implement Basic Authentication in Asp.net Web API 2.0 ? (Part 8)


As the name suggest, it is the most simple and basic process of doing authentication of HTTP Request in asp.net Web API.
In this process client sends the Base64-encoded credentials to the authorize header on every HTTP request, and after verification process Web API return the expected HTTP Response

In this process, it doesn’t use Session or cookies.

Step 1: Create the BasicAuthenticationAttribute class and write code for doing validation like given below


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http.Filters;

namespace WebApi_Sample.Security
{
    public class BasicAuthenticationAttribute: AuthorizationFilterAttribute
    {
        public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            if (actionContext.Request.Headers.Authorization == null)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
            else
            {
                // Gets header parameters  
                string authenticationString = actionContext.Request.Headers.Authorization.Parameter;
                string originalString = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationString));

                // Gets username and password  
                string usrename = originalString.Split(':')[0];
                string password = originalString.Split(':')[1];

                // Validate username and password  
                if (!ValidateUser.VaidateUser(usrename, password))
                {
                    // returns unauthorized error  
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
                }
            }

            base.OnAuthorization(actionContext);
        }

    }
}

Step 2: Create the ValidateUser Class and write the code for doing validation in database on basis of UserName and password

namespace WebApi_Sample.Security
{
    public class ValidateUser
    {
        public static bool VaidateUser(string username, string password)
        {
            // Check if it is valid credential  
            // Here we have just hardcoded the value 
            if (username.Equals("Admin") && password.Equals("Admin")) 
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

Step 3: Use the basic BasicAuthentication Attribute on the Controller as given below

Step 4. Now lunch the Postman tool and select the basic authentication option and pass the username and password as given below image

You will the get the excepted data as output.