I'm developing a Blazor Server Application (.NET 8) which uses authentication.I configured the project to use authentication this way in Program.cs:
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));builder.Services.AddCascadingAuthenticationState();builder.Services.AddAuthorization(policy => policy.FallbackPolicy = policy.DefaultPolicy);app.UseAuthentication();app.UseAuthorization();And marked the pages with the Authorize attribute in _Imports.razor:
@attribute [Authorize]First time when I started debugging the application redirected to the Microsoft login page automatically and after successfully login I could get the username through the AuthenticationStateProvider.GetAuthenticationStateAsync method.
I have points which are unclear:
How can I navigate to the Microsoft login page manually to switch user?
How can I log out the user? When I restart the debugging I'm still logged in.
I need other user data from my database but only if user is logged in. I don't want to get these data every time when user refreshes a page. Can I store these data somewhere and call the data loading when user is logged in and these data are missing?