Blazor Web Assembly with Azure AAD and Custom authentication Login

Reishabh Saxena 1 Reputation point
2022-06-02T21:42:30.053+00:00

I want to implement two authentication mechanism in my Blazor web assembly application (Azure AD and Custom Authentication). i.e. User can either login via Azure AD or he can go for Custom Authentication. When I register both Custom AuthenticationStateProvider and AddMsalAuthentication in program.cs and try to login via AAD that time I am getting below error.

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Specified cast is not valid.
System.InvalidCastException: Specified cast is not valid.
   at Microsoft.Extensions.DependencyInjection.WebAssemblyAuthenticationServiceCollectionExtensions.<>c__0`3[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteAuthenticationState, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=5.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=5.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=5.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,391 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bob Maes 1 Reputation point
    2022-12-28T10:18:00.107+00:00

    Hello,

    encountered the same issue. Did someone find a solution for this problem and would like to share this?
    Can't find the root cause of this exception...

    Kind regards,

    0 comments No comments

  2. Chris Christensen 1 Reputation point
    2023-01-06T21:57:01.283+00:00

    I'm getting the same error, but I'm doing OIDC authentication. It works fine in a template app where you run dotnet new blazorwasm --auth individual. But when I port the code over to my big app, I get this. I'm not sure where to look to figure out the problem. I'm using dotnet 6.0.112. I'll report back if I figure anything out.

    0 comments No comments

  3. Timothy Provencio 0 Reputation points
    2024-03-28T15:57:10.46+00:00

    Having the same exact issue with the same situation. Did anyone ever come up with a solution or even what the cause was?

    0 comments No comments