X.Bff
1.9.3
dotnet add package X.Bff --version 1.9.3
NuGet\Install-Package X.Bff -Version 1.9.3
<PackageReference Include="X.Bff" Version="1.9.3" />
<PackageVersion Include="X.Bff" Version="1.9.3" />
<PackageReference Include="X.Bff" />
paket add X.Bff --version 1.9.3
#r "nuget: X.Bff, 1.9.3"
#:package X.Bff@1.9.3
#addin nuget:?package=X.Bff&version=1.9.3
#tool nuget:?package=X.Bff&version=1.9.3
BFF (Backend For Frontend) 服务使用说明文档
概述
BFF 是一种设计模式,旨在为前端应用提供一个定制化的后端服务。本项目通过扩展 ASP.NET Core 的 IServiceCollection 和 IEndpointRouteBuilder 接口,提供了对 BFF 模式的原生支持。本文档将指导您如何在 ASP.NET Core 应用中配置和使用 BFF。
代码和示例是建立在 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 | Versions 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. |
-
net10.0
- Duende.AccessTokenManagement.OpenIdConnect (>= 4.1.0)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 10.0.0)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 10.0.0)
- Yarp.ReverseProxy (>= 2.1.0)
-
net8.0
- Duende.AccessTokenManagement.OpenIdConnect (>= 3.1.0)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 8.0.1 && <= 8.0.22)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 8.0.0 && <= 8.0.22)
- Yarp.ReverseProxy (>= 2.1.0)
-
net9.0
- Duende.AccessTokenManagement.OpenIdConnect (>= 3.1.0)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 9.0.0 && <= 9.0.11)
- Microsoft.Extensions.Caching.StackExchangeRedis (>= 9.0.0 && <= 9.0.11)
- Yarp.ReverseProxy (>= 2.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.