1

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?

4
  • Installed the related package and added the mentioned code in Program.cs without any issues. Make sure you have installed the package in the same ( Client/Server) Application. Commented Jan 11, 2023 at 10:28
  • Once check the related .csproj file. Commented Jan 11, 2023 at 10:28
  • 1
    My project Folder structure. Commented Jan 11, 2023 at 10:32
  • Did you ever get it working? If so, could you share your findings? Commented Jan 25 at 0:29

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.