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

AuthorizeView does not use custom AuthenticationStateProvider in Blazor Server

$
0
0

I have created a custom AuthenticationStateProvider that derives from ServerAuthenticationStateProvider. So far this class doesn't do anything special, but it will eventually contain some custom functionality.

public class MyAuthenticationStateProvider : ServerAuthenticationStateProvider{    public MyAuthenticationStateProvider ()    {    }    public override async Task<AuthenticationState> GetAuthenticationStateAsync()    {        //Place breakpoint here        var claimsIdentity = new ClaimsIdentity(new Claim[]        {             new Claim(ClaimTypes.Name, "Example User")        }, "Example");        var exampleUser = new ClaimsPrincipal(claimsIdentity);        var authState = new AuthenticationState(exampleUser);        //Without this line, the AuthorizeView won't update.        this.SetAuthenticationState(Task.FromResult(authState));        return authState;    }}

I register this class in the DI container as follows.

builder.Services.AddScoped<AuthenticationStateProvider, MyAuthenticationStateProvider>();

I also register a cascading auth state

builder.Services.AddCascadingAuthenticationState();

In my MainLayout.razor, I am using an AuthorizeView

<AuthorizeView Context="AuthContext"><Authorized><small>Signed in as @(AuthContext.User?.Identity?.Name ?? "Unknown")</small></Authorized><NotAuthorized><small>No login detected.</small></NotAuthorized></AuthorizeView>

When I run my app, I always see "No login detected". The breakpoint on the first line of GetAuthenticationStateAsync in MyAuthenticationStateProvider is never hit.

However, if I manually inject MyAuthenticationStateProvider, and call GetAuthenticationStateAsync everything works. The AuthorizeView behaves as expected and displays "Signed in as Example User" after prerender. The breakpoint is hit twice, once for prerender, and once for interactive.

 @code {    [Inject]    public AuthenticationStateProvider? AuthenticationStateProvider { get; set; }    protected override async Task OnInitializedAsync()    {        if (AuthenticationStateProvider != null)        {            //If I comment this line, MyAuthenticationStateProvider is *never* called            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();        }    }}

I am wondering why I have to call GetAuthenticationStateAsync myself in order for it to work. I feel as though I shouldn't have to call SetAuthenticationState in my Get method. Where does AuthorizeView get it's initial AuthenticationState from, if not from the registered AuthenticationStateProvider? My assumption would be that any time authentication state is needed, it would always use the registered AuthenticationStateProvider dependency. Is there some other places I need to set the initial Authentication state?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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