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

AuthorizeRouteView Authorizing and NotAuthorized parameters settings

$
0
0

I wanted to use the NotAuthorized attribute in the <AuthorizeRouteView> tag to redirect to the login page every time a non logged-in user try to access a page.

Howewer, it requires a RenderFragment<AuthentificationState> typed parameter. What shoud I put to set this parameter to render the login page ?

Edit : Code is pretty simple. I used the Blazor server side project template with identity stored in application, just added the RedirectToLogin.razor like this :

@inject NavigationManager NavigationManager@code {     protected override void OnAfterRender()    {        NavigationManager.NavigateTo("counter"); //for an unknown reason, the "Identity/Account/Login" redirect doesn't work.    }}

And modified the App.razor :

<CascadingAuthenticationState><Router AppAssembly="@typeof(Program).Assembly"><Found Context="routeData"><AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"><NotAuthorized>                    @if(true) { } //Used for breakpoint.<RedirectToLogin /></NotAuthorized><Authorizing>                    @if(true) { } //Used for breakpoint.</Authorizing></AuthorizeRouteView></Found><NotFound><LayoutView Layout="@typeof(MainLayout)"><p>Sorry, there's nothing at this address.</p></LayoutView></NotFound></Router></CascadingAuthenticationState>

I didn't touch the Startup.cs so it look like this :

public class Startup    {        public Startup(IConfiguration configuration)        {            Configuration = configuration;        }        public IConfiguration Configuration { get; }        // This method gets called by the runtime. Use this method to add services to the container.        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940        public void ConfigureServices(IServiceCollection services)        {            services.AddDbContext<ApplicationDbContext>(options =>                options.UseSqlServer(                    Configuration.GetConnectionString("DefaultConnection")));            services.AddDefaultIdentity<IdentityUser>()                .AddEntityFrameworkStores<ApplicationDbContext>();            services.AddRazorPages();            services.AddServerSideBlazor();            services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();            services.AddSingleton<WeatherForecastService>();        }        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)        {            if (env.IsDevelopment())            {                app.UseDeveloperExceptionPage();                app.UseDatabaseErrorPage();            }            else            {                app.UseExceptionHandler("/Error");                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.                app.UseHsts();            }            app.UseHttpsRedirection();            app.UseStaticFiles();            app.UseRouting();            app.UseAuthentication();            app.UseAuthorization();            app.UseEndpoints(endpoints =>            {                endpoints.MapControllers();                endpoints.MapBlazorHub();                endpoints.MapFallbackToPage("/_Host");            });        }    }

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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