c# - After implementing Duende BFF deployed web app not working - Stack Overflow

admin2025-04-15  2

I followed the sample project bff react on our project. Our project is an asp core web application (8) with spa (react and vite). On local development environment it is working fine. After deploying project to folder homepage gives me HTTP 404 error.

Code changes on Program.cs

        builder.Services.AddBff();
        builder.Services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            options.DefaultSignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
        }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
        {
            //strict SameSite Handling
            options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
            options.Cookie.Name = "__dmmService-bff";
        }).AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            options.Authority = iamAuthConfig.Authority;
            options.ClientId = iamAuthConfig.ClientId;
            options.ClientSecret = iamAuthConfig.ClientSecret;
            options.ResponseType = OpenIdConnectResponseType.Code;
            options.ResponseMode = OpenIdConnectResponseMode.Query;

            options.GetClaimsFromUserInfoEndpoint = true;
            options.MapInboundClaims = false;
            options.SaveTokens = true;
            options.DisableTelemetry = true;

            options.Scope.Clear();
            foreach (var scope in iamAuthConfig.Scopes.Split(" "))
            {
                options.Scope.Add(scope);
            }

            options.TokenValidationParameters = new()
            {
                NameClaimType = "name",
                RoleClaimType = "role"
            };
        }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
        {
            options.SaveToken = true;
        })
        builder.Services.AddAuthorizationBuilder().AddPolicy(....);
        if (environment.IsDevelopment())
        {
            app.UseDefaultFiles();
            app.UseStaticFiles();
        }
        //it was already there
        app.UseRouting();
        app.UseAuthentication();
        //new added
        app.UseBff();
        //it was already there
        app.UseAuthorization();
        //new added
        app.MapBffManagementEndpoints();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute().RequireAuthorization();
        });
        app.MapFallbackToFile("/index.html");

`

I followed the sample project bff react on our project. Our project is an asp.net core web application (.net8) with spa (react and vite). On local development environment it is working fine. After deploying project to folder homepage gives me HTTP 404 error.

Code changes on Program.cs

        builder.Services.AddBff();
        builder.Services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            options.DefaultSignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
        }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
        {
            //strict SameSite Handling
            options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
            options.Cookie.Name = "__dmmService-bff";
        }).AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
        {
            options.Authority = iamAuthConfig.Authority;
            options.ClientId = iamAuthConfig.ClientId;
            options.ClientSecret = iamAuthConfig.ClientSecret;
            options.ResponseType = OpenIdConnectResponseType.Code;
            options.ResponseMode = OpenIdConnectResponseMode.Query;

            options.GetClaimsFromUserInfoEndpoint = true;
            options.MapInboundClaims = false;
            options.SaveTokens = true;
            options.DisableTelemetry = true;

            options.Scope.Clear();
            foreach (var scope in iamAuthConfig.Scopes.Split(" "))
            {
                options.Scope.Add(scope);
            }

            options.TokenValidationParameters = new()
            {
                NameClaimType = "name",
                RoleClaimType = "role"
            };
        }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
        {
            options.SaveToken = true;
        })
        builder.Services.AddAuthorizationBuilder().AddPolicy(....);
        if (environment.IsDevelopment())
        {
            app.UseDefaultFiles();
            app.UseStaticFiles();
        }
        //it was already there
        app.UseRouting();
        app.UseAuthentication();
        //new added
        app.UseBff();
        //it was already there
        app.UseAuthorization();
        //new added
        app.MapBffManagementEndpoints();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute().RequireAuthorization();
        });
        app.MapFallbackToFile("/index.html");

`

Share Improve this question asked Feb 4 at 10:10 Atahan CeylanAtahan Ceylan 1683 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I changed the orders of middleware and deleted some unnecessary parts like app.MapFallbackToFile("/index.html"); Now it is working. Working Program.cs below:

    builder.Services.AddBff();
    builder.Services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        options.DefaultSignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
    }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
    {
        //strict SameSite Handling
        options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
        options.Cookie.Name = "__dmmService-bff";
    }).AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
    {
        options.Authority = iamAuthConfig.Authority;
        options.ClientId = iamAuthConfig.ClientId;
        options.ClientSecret = iamAuthConfig.ClientSecret;
        options.ResponseType = OpenIdConnectResponseType.Code;
        options.ResponseMode = OpenIdConnectResponseMode.Query;

        options.GetClaimsFromUserInfoEndpoint = true;
        options.MapInboundClaims = false;
        options.SaveTokens = true;
        options.DisableTelemetry = true;

        options.Scope.Clear();
        foreach (var scope in iamAuthConfig.Scopes.Split(" "))
        {
            options.Scope.Add(scope);
        }

        options.TokenValidationParameters = new()
        {
            NameClaimType = "name",
            RoleClaimType = "role"
        };
    }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
    {
        options.SaveToken = true;
    })
    builder.Services.AddAuthorizationBuilder().AddPolicy(....);
    app.UseBff();
    app.MapBffManagementEndpoints();
转载请注明原文地址:http://www.anycun.com/QandA/1744728238a86790.html