Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Custom AuthenticationStateProvider not working

$
0
0

I've implemented this class in my Blazor server project (using .net 6.0).

public class CustomAuthenticationStateProvider : AuthenticationStateProvider{    public CustomAuthenticationStateProvider(ProtectedSessionStorage sessionStorage)    {        _sessionStorage = sessionStorage;    }    // also contains working code which isn't relevant to this problem (so not shown for brevity).}

The DI startup code looks like this:

builder.Services.AddAuthenticationCore();builder.Services.AddRazorPages();builder.Services.AddScoped<ProtectedSessionStorage>();builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();

However, when trying to access this from the login page (a razor component), the CustomAuthenticationStateProvider is not found.

Markup (relevant lines only):

@inject AuthenticationStateProvider authStateProvider<div class="button login" @onclick="() => DoLogin()">   Login</div>

The method which errors in the markup:

private async Task DoLogin(){    var customAuthStateProvider = authStateProvider as CustomAuthenticationStateProvider;    await customAuthStateProvider.UpdateAuthenticationState(new UserSession    {        UserName = _inputUsername,        Role = "admin"    });}

However, customAuthStateProvider is always null because authStateProvider (which is injected into the razor component) is not the scoped instance of CustomAuthenticationStateProvider ... it's just some other instance of AuthenticationStateProvider.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>