0

I'm getting the following exception when attempting to browse to my site :

enter image description here

The configuration/staticcontent endpoint is implemented like so:

    [HttpGet("staticcontent")]
    public async Task<IActionResult> GetStaticContent()
    {
        return Ok(this.mapper.Map<StaticContentValueDto[]>(await this.staticContentValuesProvider.GetStaticContentValues()));
    }

.. .and the implementation of GetStaticContentValues() as follows:

    public async Task<IEnumerable<StaticContentValue>> GetStaticContentValues()
    {
        return await this.dbContext.StaticContentValues.ToArrayAsync();
    }

I suspect there might be an issue with AD authentication?

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry();
        services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });
        services.AddMvcCore()
            .AddAuthorization();

        services.AddControllers().AddNewtonsoftJson(o=>
        {
            o.SerializerSettings.ContractResolver = new DefaultContractResolver();
            o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        });
        

        string connection = Configuration.GetConnectionString("DefaultConnection");
        string reportingConnection = Configuration.GetConnectionString("ReportingConnection");

        services.AddDbContext<PnbIdentityDbContext>(options =>
            options.UseSqlServer(connection));
        
        //20 or so other adddbcontext for sql server here
    //20 or so other adddbcontext for sql server here
        
        services.AddIdentity<PnbIdentityUser, PnbIdentityRole>(options => {
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 8;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;
        })
            .AddEntityFrameworkStores<PnbIdentityDbContext>()
            .AddDefaultTokenProviders();

        services.AddAutoMapper(typeof(CapsAutoMapperProfile));

        SessionConfigurator.Configure(services);
        AuthConfigurator.Configure(services, Configuration["Identity:TokenSecret"]);
        DiConfigurator.Configure(services);
        HangfireConfigurator.Configure(services, connection);
    }

Please note that Identity:TokenSecret is set in appsettings.json.

What am I doing wrong? What is the reason for the 401 response?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • Since there is no authorization attribute on your HTTP method, I would check the authentication for your static web site in your App Service (in Azure Portal) is set to Anonymous and not to an authentication provider. – Andrew Halil Sep 13 '21 at 00:22
  • im having a tough time finding where to check the authentication for the static web site. – Alex Gordon Sep 13 '21 at 01:16
  • If we talking off the shelf auth then its in the config file within the service. Check out this link: https://learn.microsoft.com/en-us/azure/static-web-apps/configuration-overview. Right at the top talks about application config and how it affects authentication/authorization. Hopefully it helps (if it does let us know and i'll put it up as answer) – Aeseir Sep 13 '21 at 01:29
  • @AndrewHalil any chance you can point me to where i can configure authentication ? – Alex Gordon Sep 13 '21 at 16:14
  • @Aeseir i think im following what you're saying but i dont see where to configure this in the portal – Alex Gordon Sep 13 '21 at 16:15
  • ill post a 500 bounty asap – Alex Gordon Sep 13 '21 at 16:47
  • @AlexGordon you need to create the file and then upload it to the appropriate location. Have a ready of this link https://learn.microsoft.com/en-au/azure/static-web-apps/application-settings#uploading-application-settings – Aeseir Sep 14 '21 at 04:43
  • Btw I am assuming you are using Azure Static Web App as opposed to Static Web App via Azure Storage – Aeseir Sep 14 '21 at 04:44
  • To authentication and authorization on your azure static webapp please refer this link https://learn.microsoft.com/en-us/azure/static-web-apps/authentication-authorization – AjayKumarGhose Sep 14 '21 at 09:51
  • Also you can refer this links for proper error handling https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-5.0 , https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-5.0 , https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/error-handling/samples . May it help – AjayKumarGhose Sep 28 '21 at 10:13

1 Answers1

0

any chance you can point me to where i can configure authentication ?

Thank you Ken W MSFT , Posting your suggestion as answer to help other community members.

"Yes, it is possible. Azure Static Web Apps provides a streamlined authentication experience. By default, you have access to a series of pre-configured providers, or the option to register a custom provider.

  • Any user can authenticate with an enabled provider. Once logged in, users belong to the anonymous and authenticated roles by default.
  • Authorized users gain access to restricted routes by rules defined in the staticwebapp.config.json file. Users join custom roles via provider-specific invitations, or through a custom Azure Active
  • Directory provider registration.
  • All authentication providers are enabled by default.
    • To restrict an authentication provider, block access with a custom route rule.
  • Pre-configured providers include:
    • Azure Active Directory
    • GitHub
    • Twitter

Reference: https://learn.microsoft.com/en-us/azure/static-web-apps/authentication-authorization ''

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15