X.Bff 1.9.3

dotnet add package X.Bff --version 1.9.3
                    
NuGet\Install-Package X.Bff -Version 1.9.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="X.Bff" Version="1.9.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="X.Bff" Version="1.9.3" />
                    
Directory.Packages.props
<PackageReference Include="X.Bff" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add X.Bff --version 1.9.3
                    
#r "nuget: X.Bff, 1.9.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package X.Bff@1.9.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=X.Bff&version=1.9.3
                    
Install as a Cake Addin
#tool nuget:?package=X.Bff&version=1.9.3
                    
Install as a Cake Tool

BFF (Backend For Frontend) 服务使用说明文档

概述

  1. BFF 是一种设计模式,旨在为前端应用提供一个定制化的后端服务。本项目通过扩展 ASP.NET Core 的 IServiceCollection 和 IEndpointRouteBuilder 接口,提供了对 BFF 模式的原生支持。本文档将指导您如何在 ASP.NET Core 应用中配置和使用 BFF。

  2. 代码和示例是建立在 OidcProxy.Net、 Fhi.AuthExtensions、 IdentityServer4 等开源项目之上的。

The code and examples are built on top of open-source projects like OidcProxy.Net, Fhi.AuthExtensions, IdentityServer4, etc.

配置与初始化

添加 BFF 支持

首先,您需要在 Startup.cs 或者 Program.cs 文件中的 ConfigureServices 方法内调用 AddBff 方法来添加 BFF 支持:

services.AddBff(config, options =>
{
    // 在这里配置您的 BFFOptions
});
  • config: ReverseProxyOptions 类型的实例,用于配置反向代理。
  • configureOptions: 一个 Action<BffOptions> 委托,用于配置 BFF 相关选项。

此方法会自动注册必要的服务,并配置分布式缓存、身份验证处理程序等。

映射 BFF 端点

接下来,在 Configure 方法或 UseEndpoints 方法中调用 MapBffEndpoints 来映射默认的 BFF 端点:

app.MapBffEndpoints();

or

app.UseEndpoints(endpoints =>
{
    endpoints.MapBffEndpoints();
});

这会自动为您映射 /login, /me, /logout, 和 /back-channel-logout 端点。

注册的端点:

端点路径 HTTP 方法 描述 认证要求
{prefix}/login GET 登录端点 匿名访问
{prefix}/me GET 用户信息端点 匿名访问
{prefix}/logout GET 登出端点 匿名访问
{prefix}/back-channel-logout POST 后台登出处理 匿名访问
- - 反向代理端点 自动配置

注意:{prefix} 由 BffOptions.EndpointPrefix 配置,截止到现在,Openiddict 授权框架并未支持 Backchannel logout,详情请看此 issue 并在其中找到 SSO logout 的 解决方案。

扩展方法

BFF 提供了几个扩展方法以方便进一步定制化:

AddDefaultCookie

允许您为默认的身份验证方案配置 Cookie 认证选项:

bffBuilder.AddDefaultCookie(options =>
{
    // 配置 CookieAuthenticationOptions
});

AddDefaultOpenIdConnect

允许您为 OpenID Connect 身份验证方案进行配置:

bffBuilder.AddDefaultOpenIdConnect(options =>
{
    // 配置 OpenIdConnectOptions
});

AddRedisCache

如果您希望使用 Redis 作为分布式缓存而非内存缓存,可以使用该方法:

bffBuilder.AddRedisCache(options =>
{
    // 配置 RedisCacheOptions
});

结论

通过上述步骤,您可以轻松地在您的 ASP.NET Core 应用中集成 BFF 设计模式。这个模式特别适用于微服务架构下的前端应用,它能够有效地简化前端与多个后端服务之间的通信复杂度,同时还能增强安全性。根据您的具体需求,您可以进一步自定义和扩展所提供的功能。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.9.3 266 12/7/2025
1.9.2 204 7/14/2025