最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 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");

`

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

2 Answers 2

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();

You should change StaticFiles middlewares setting like below.

if (environment.IsDevelopment())
{
    app.UseDefaultFiles();
    app.UseStaticFiles();
}else{
    app.UseDefaultFiles();
    app.UseStaticFiles();
}

After deployment is complete, we need to ensure that the wwwroot folder is included in the release file.

发布评论

评论列表(0)

  1. 暂无评论