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

Viewed 773

When I deploy a NET 6 blazor web app (server and client) into IIS but I got this error when I run it, http://blazorecommerceapi.findingsteve.net

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

enter image description here

When I run its api, http://blazorecommerceapi.findingsteve.net/api/product

enter image description here

But when I run it locally, it works as expected. I can run the blazor client page.

enter image description here

I can see the api data.

enter image description here

I am using Sqlite to persist the data and for the authentication, these are some codes in program.cs

builder.Services.AddScoped<IAuthService, AuthService>();

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey =
                new SymmetricSecurityKey(System.Text.Encoding.UTF8
                .GetBytes(builder.Configuration.GetSection("AppSettings:Token").Value)),
            ValidateIssuer = false,
            ValidateAudience = false
        };
    });

app.UseAuthentication();
app.UseAuthorization();
0 Answers
Related