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

MAUI Blazor Hyrid App Auth Not Working (.NET 8)

$
0
0

I have a new application that I am attempting to setup the Microsoft.AspNetCore.Components.Authorization components in. I have followed the guide from Microsoft here (see the Create a custom AuthenticationStateProvider with user change updates section). I have created the custom AuthenticationStateProvider implementation, added the AuthorizeRouteVie components to my routes, added the NotAuthorized child component for AuthorizeRouteView, I have added the CascadingAuthenticationState component, I have setup the necessary services in MauiProgram.cs, and I have added @attribute [Authorize] to _Imports.razor to secure all pages. However, when I run the application it still loads the MainLayout.razor with it's page components that should only be accessible by an authorized user. I have seen this message in the console:

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService: Information: Authorization failed. These requirements were not met: DenyAnonymousAuthorizationRequirement: Requires an authenticated user.

VetPlusAuthenticationStateProvider.cs

public class VetPlusAuthenticationStateProvider : AuthenticationStateProvider{    private AuthenticationState currentUser;    public VetPlusAuthenticationStateProvider(VetPlusAuthenticationService service)    {        currentUser = new AuthenticationState(service.CurrentUser);        service.UserChanged += (newUser) =>        {            currentUser = new AuthenticationState(newUser);            NotifyAuthenticationStateChanged(Task.FromResult(currentUser));        };    }    public override Task<AuthenticationState> GetAuthenticationStateAsync() =>        Task.FromResult(currentUser);}

VetPlusAuthenticationService.cs

public class VetPlusAuthenticationService{    public event Action<ClaimsPrincipal>? UserChanged;    private ClaimsPrincipal? currentUser;    public ClaimsPrincipal CurrentUser    {        get { return currentUser ?? new(); }        set        {            currentUser = value;            if (UserChanged is not null)            {                UserChanged(currentUser);            }        }    }}

Routes.razor

<CascadingAuthenticationState><Router AppAssembly="typeof(Routes).Assembly"><Found Context="routeData"><AuthorizeRouteView RouteData="routeData"  DefaultLayout="typeof(Layout.MainLayout)"><NotAuthorized><LoginRedirect /></NotAuthorized><Authorizing><h1>I AM AUTHORIZING</h1></Authorizing></AuthorizeRouteView></Found><NotFound><LayoutView Layout="@typeof(Layout.MainLayout)"><p>Sorry, there's nothing at this address.</p></LayoutView></NotFound></Router></CascadingAuthenticationState>

_Imports.razor

@using System.Net.Http@using System.Net.Http.Json@using Microsoft.AspNetCore.Authorization@using Microsoft.AspNetCore.Components.Authorization@using Microsoft.AspNetCore.Components.Forms@using Microsoft.AspNetCore.Components.Routing@using Microsoft.AspNetCore.Components.Web@using static Microsoft.AspNetCore.Components.Web.RenderMode@using Microsoft.AspNetCore.Components.Web.Virtualization@using Microsoft.JSInterop@using VetPlus.Shared@using VetPlus.Shared.Layout@using VetPlus.Shared.Pages@using VetPlus.Shared.Components@using VetPlus.Shared.Components.Redirect@using VetPlus.Shared.Utils@using DevExpress.Blazor@attribute [Authorize]

MauiProgram.cs

public static class MauiProgram{    public static MauiApp CreateMauiApp()    {        var builder = MauiApp.CreateBuilder();        builder            .UseMauiApp<App>()            .ConfigureFonts(fonts =>            {                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");            });        builder.Services.AddMauiBlazorWebView();        builder.Services.AddDevExpressBlazor(options => {            options.BootstrapVersion = BootstrapVersion.v5;            options.SizeMode = DevExpress.Blazor.SizeMode.Medium;        });        builder.Services.AddOptions();        builder.Services.AddAuthorizationCore();        builder.Services.AddSingleton<VetPlusAuthenticationService>();        builder.Services.AddScoped<AuthenticationStateProvider, VetPlusAuthenticationStateProvider>();        builder.Services.AddCascadingAuthenticationState(); #if DEBUG        builder.Services.AddBlazorWebViewDeveloperTools();        builder.Logging.AddDebug(); #endif        builder.Services.AddSingleton<IPracticeHubClient, PracticeHubClient>();        return builder.Build();    }}

I am really unsure what I am missing here. I am uncertain how this affects the Microsoft guide, but all of the razor components for the project are in a Razor Class Library project that is referenced by the Maui project. I also noticed that I needed to adda reference to Microsoft.AspNetCore.Authorization in order to use the @attribute [Authorize] attribute declaration. Is this package not compatible with the Maui project?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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