Invalid Sample for Custom AuthenticationStateProvider
See original GitHub issueWhen implemented as described here, the following exception is thrown:
Exceptions
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.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.AspNetCore.Components.WebAssembly.Authentication.RemoteUserAccount, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=5.0.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Authentication.WebAssembly.Msal.Models.MsalProviderOptions, Microsoft.Authentication.WebAssembly.Msal, Version=5.0.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].<AddRemoteAuthentication>b__0_1(IServiceProvider sp)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=5.0.0.1, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
Further technical details
Used Visual Studio 2019.
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0' ">
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="5.0.4" />
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="5.0.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup>
Cross-reference: https://github.com/dotnet/AspNetCore.Docs/issues/21802 cc: @guardrex
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Correct implementation of custom ...
The documentation has failed in this case because it explains how to implement a custom AuthenticationStateProvider but not on how to create and ......
Read more >Customizing the AuthenticationStateProvider in Blazor ...
The following code sample describes how to create a custom AuthenticationStateProvider object whose objective is to retrieve from the local ...
Read more >You're using AuthenticationStateProvider wrong in your Blazor ...
The AuthenticationStateProvider service in in Blazor Server the main point to get information about the authentication state of the current ...
Read more >ASP.NET Core Blazor authentication and authorization
To handle authentication, use of the built-in or custom AuthenticationStateProvider service is covered in the following sections.
Read more >Blazor Web Assembly with Azure AAD and Custom ...
When I register both Custom AuthenticationStateProvider and ... InvalidCastException: Specified cast is not valid. at Microsoft.Extensions.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have that exact problem, and I’ve tried to implement both IRemoteAuthenticationService and IAccessTokenProvider, ending up with a so messy copy and paste of code that I eventually give up. I put a switch on configuration that allows the application to be authenticated EITHER through custom auth OR MSAL. I do agree that this is a pretty basic requirement, even more urgent giving the lack of an identity server to be provided that could work-around the problem.
What would be the recommended way of solving the problem?
Thanks for contacting us. We’re moving this issue to the
Next sprint planning
milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.