0

I've already looked at this similar .NET 6 issue but none of the suggest options help: "There was an error trying to log you in: '' " Blazor WebAssembly using IdentiyServer Authentication

My issue is similar. I'm trying to simply port the configuration used in the default Blazor WebAssembly 7 template with Individual User Accounts to my existing Blazor WebAssembly project. I'm using the same approach that integrates my WebAPIs with IdentityServer and ASP.NET Core Identity.

Server Program.cs:

string? connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
  options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();


builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = false)
  .AddEntityFrameworkStores<AppDbContext>();

builder.Services.AddIdentityServer()
  .AddApiAuthorization<ApplicationUser, AppDbContext>()
  .AddDeveloperSigningCredential(); // TODO: fix this in production

builder.Services.AddAuthentication()
    .AddIdentityServerJwt();

// skipping some stuff

app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseIdentityServer();
app.UseAuthorization();

Client Program.cs:

string clientName = "Blazor.WebApi";
builder.Services.AddHttpClient(clientName, client =>
    client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
        .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
    .CreateClient(clientName));

builder.Services.AddScoped<HttpService>();

builder.Services.AddApiAuthorization();

builder.Services.AddBlazoredLocalStorage();

await builder.Build().RunAsync();

Many answers suggest adding additional configuration for oidc but this template hides all of that behind extension methods. In a File-New Project using the template, it all works, but I can't figure out why it's not working in my existing application.

Output from console when app runs:

info: Duende.IdentityServer.Startup[0]
      Starting Duende IdentityServer version 6.0.4+9dfb7e94e795f55b2c063d54d11b70aae05e4e07 (.NET 7.0.5)
warn: Duende[0]
      You do not have a valid license key for the Duende software. This is allowed for development and testing scenarios. If you are running in production you are required to have a licensed version. Please start a conversation with us: https://duendesoftware.com/contact
info: Microsoft.Extensions.DependencyInjection.ConfigureApiResources[2]
      Configuring local API resource 'Blazor.WebApiAPI'.
info: Duende.IdentityServer.Startup[0]
      Using explicitly configured authentication scheme Identity.Application for IdentityServer
info: FastEndpoints.StartupTimer[0]
      Registered 7 endpoints in 54 milliseconds.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:57101
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]

Error in browser: There was an error trying to log you in: 'No authority or metadataUrl configured on settings'

Browser Console:

info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed. These requirements were not met:
      DenyAnonymousAuthorizationRequirement: Requires an authenticated user.

The error doesn't have any network requests associated with it. The URL is /authentication/login-failed and it shows this even if I manually go to the login razor page and successfully log in. What am I missing?

ssmith
  • 8,092
  • 6
  • 52
  • 93

0 Answers0