3

Everytime I publish my Blazor Server-project to my website domain, and opening the website, this exception occurs, and there's little to no help Googling it:

enter image description here

And it says AppState.cs: line 21, so here's the codeline for it:

enter image description here

This exception is not happening under debugging localhost. When I delete localStorage from the browser on my website, and refreshing, then everything works. But I don't want my customers having this exception and having to tell them to delete the localstorage everytime I'm publishing.

My Program.cs if necessary:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor().AddCircuitOptions(options => options.DetailedErrors = true);
builder.Services.AddHttpClient();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters.ValidateIssuerSigningKey = true; // Validér secret key for JWT
        options.TokenValidationParameters.ValidateLifetime = false; // Validér ikke Lifetime på JWT
        options.TokenValidationParameters.ValidateAudience = false; // Ikke validér clients(audience), fx BlazorWeb, der skal anvende IdentityServer
        options.TokenValidationParameters.ValidateIssuer = false; // Ikke validér IdentityServer(issuer)
        options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:Secret"])); // Sæt secret key for JWT, der bruges som adgangskode til at tilgå JWT
    });

builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<AuthenticationStateProvider, AppState>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

RewriteOptions options = new RewriteOptions();
options.AddRedirectToWww();
options.AddRedirectToHttps();
app.UseRewriter(options);

app.UseStaticFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();
Zecret
  • 360
  • 1
  • 4
  • 19

2 Answers2

1

After many hours of research, I managed to fix it. I did the following; Added builder.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"c:\your\path\to\store\keys"));.

Zecret
  • 360
  • 1
  • 4
  • 19
0

Try to set Load User Profile to true in your IIS app pool in the advanced settings. see this answer, I hope that will help you!

Nb777
  • 1,658
  • 8
  • 27
  • I read that somewhere else, but I have no idea what or how to do this. This doesn't seem like something I have ever touched. Is this easily done? When I google about this, it seems like I need to download a software on my computer (IIS manager or something), and then change advanced settings? But I'm publishing from Visual Studio a Web Publish to my website domain, so I don't see why I should do this – Zecret Mar 16 '22 at 10:46
  • You need to connect with the server where you application by for example `remote desktop connection` and go to iis manager and change the setting there. – Nb777 Mar 16 '22 at 10:50
  • I don't think it's possible to remotely connect to the server of my website. Is there something in my code that's causing this issue that I'm having, do you think? Maybe I need to change IssuerSigningKey in my Program.cs? – Zecret Mar 16 '22 at 11:02
  • No, don't think. Maybe you need to change your way of Dataprotection, see: https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-3.1 – Nb777 Mar 16 '22 at 12:56