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

Blazor pipeline broken when introducing authorization

$
0
0

I've been wrestling with this for quite a while now. I have a working blazor (auto - hybrid) project called CustomerPortal and it created a CustomerPortal.Client for me as well.

I created a 2 razor components, ServerTime and ClientTime which just shows the current time and keeps on updating it. This page works fine, up to the point where i introduce authorization in my program.cs. Suddenly i get a javasscript error and the blazor interactivity is gone (my time won't update anymore and buttons will no longer work). Both the client side timer as the server side timer will stop working on this same error.

Error is as follows:
blazor addauthorization Uncaught SyntaxError: expected expression, got '<'` in the blazor.web.js file

The only thing i add to my program.cs file is this:
`

builder.Services.AddAuthorization(options =>{    options.FallbackPolicy = options.DefaultPolicy;});

This seems to put the blazor interactivity files behind authorization as well. I have tried numerous solutions but I keep getting the same error. if i remove these lines it works again. Does anyone know how to fix this?

My full Program.cs for transparency:
`

using CustomerPortal.Middleware;using CustomerPortal.Services;using CustomerPortal.Components;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Components.Authorization;using Microsoft.AspNetCore.Components.Server;using Web.Core;var builder = WebApplication.CreateBuilder(args);var env = builder.Environment;// AuthN/Zbuilder.Services.AddAuthentication("PortalCookie")    .AddCookie("PortalCookie", o =>    {        o.LoginPath = "/login";        o.LogoutPath = "/logout";        o.Cookie.HttpOnly = true;        //only send cookie over HTTPS, we use the development block to keep it running on development since we run on http there        o.Cookie.SecurePolicy = env.IsDevelopment()            ? CookieSecurePolicy.None            : CookieSecurePolicy.Always;        o.Cookie.SameSite = SameSiteMode.Strict;        o.SlidingExpiration = true;        o.ExpireTimeSpan = TimeSpan.FromMinutes(60);    });builder.Services.AddAuthorization(options =>{    options.FallbackPolicy = options.DefaultPolicy;});// Add services to the container.builder.Services.AddRazorComponents()    .AddInteractiveServerComponents()    .AddInteractiveWebAssemblyComponents();builder.Services.AddHttpContextAccessor();// Blazor Server helpers (mag blijven)builder.Services.AddRazorPages();//builder.Services.AddServerSideBlazor();// Controllers (voor /login & /logout)builder.Services.AddControllersWithViews();builder.Services.AddHttpContextAccessor();builder.Services.AddScoped<UserContext>();builder.Services.AddSingleton<FullUserCache>();// HttpClient naar IDPbuilder.Services.AddHttpClient("IDP", c =>{    var baseUrl = builder.Configuration["Idp:BaseUrl"];    if (string.IsNullOrWhiteSpace(baseUrl))    {        baseUrl = builder.Environment.IsDevelopment()            ? "https://localhost:7024"            : "https://idp.domainname.eu";    }    c.BaseAddress = new Uri(baseUrl);});// Login servicebuilder.Services.AddScoped<ILoginService, LoginService>();builder.Services.AddCascadingAuthenticationState();builder.Services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();builder.Services.AddDistributedMemoryCache();builder.Services.AddSession(options =>{    options.IdleTimeout = TimeSpan.FromMinutes(60);    options.Cookie.HttpOnly = true;    options.Cookie.IsEssential = true;});var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){    app.UseWebAssemblyDebugging();}else{    app.UseExceptionHandler("/Error", createScopeForErrors: true);    // 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.UseAntiforgery();     app.UseSession();app.UseAuthentication();app.UseAuthorization();app.UseMiddleware<EnsureUserContextMiddleware>();app.MapControllers();app.MapRazorComponents<App>()    .AddInteractiveServerRenderMode()    .AddInteractiveWebAssemblyRenderMode()    .AddAdditionalAssemblies(typeof(CustomerPortal.Client._Imports).Assembly);app.MapGet("/logout", async (HttpContext ctx, ILoginService login) =>{    await login.LogoutAsync(ctx);    return Results.Redirect("/");});app.Run();

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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