Home > other >  Adding Azure Active Directory authentication to a Blazor WebAssembly app
Adding Azure Active Directory authentication to a Blazor WebAssembly app

Time:01-12

I've got an existing Blazor Web Assembly app and I'm attempting to implement Azure AD authentication so that only users who have signed in with their work account can access the web app.

I'm attempting to follow this Microsoft guide. I've got the Microsoft.Authentication.WebAssembly.Msal nuget package installed however, in my Program.cs, I receive the following error:

IServiceCollection does not contain a definition for AddMsalAuthentication.

for the following block of code:

builder.Services.AddMsalAuthentication(options =>
    {
        builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    });

Any suggestions on why and how to resolve this?

CodePudding user response:

In Visual Studio, created the Blazor WebAssembly App.

enter image description here

In Program.cs file, added the code which you have mentioned.

builder.Services.AddMsalAuthentication(options =>
{
    builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
});

Initially even I got the same error.

enter image description here

In BlazorApp1.Server Application, Installed the Microsoft.Authentication.WebAssembly.Msal NuGet Package.

<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="7.0.2" />
  • Now I am able to build the application without any issues.

My BlazorApp1.Server .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>BlazorApp1.Server-****</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.0" />
    <PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="7.0.2" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Client\BlazorApp1.Client.csproj" />
    <ProjectReference Include="..\Shared\BlazorApp1.Shared.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0" />
  </ItemGroup>
</Project>

My Folder Structure:

enter image description here

  • As there are two Program.cs files (Client/Server), make sure you have installed the package in the application where you are adding the code.
  • Related