Search code examples
blazorasp.net-authorizationblazor-client-side

Blazor wasm unable to resolve AuthorizationOptions while attempting to activate DefaultAuthorizationPolicyProvider


I'm working on a Blazor wasm application and ran into an exception when adding the AuthorizeRouteView component in the App.razor file. I lost quite some time trying to solve as there are few resources about this, so I want to share the solution here:

Error message was:

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Options.IOptions`1[Microsoft.AspNetCore.Authorization.AuthorizationOptions]' while attempting to activate 'Microsoft.AspNetCore.Authorization.DefaultAuthorizationPolicyProvider'.

Package versions:

    <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.2.0-preview1.20073.1" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.2.0-preview1.20073.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.1" PrivateAssets="all"/>

Following descriptions I only had to add AddAuthorizationCore() in the Program.cs Services:

builder.Services.AddAuthorizationCore();

This then produces the above exception...


Solution

  • The Solution

    When I first added the AddOptions() to the Services in the Program.cs, the problem was solved:

    builder.Services.AddOptions();
    builder.Services.AddAuthorizationCore();