Skip to content

Commit f7501fe

Browse files
committed
BFF v4 - Document Simplified Wireup
1 parent ed09f00 commit f7501fe

File tree

8 files changed

+333
-194
lines changed

8 files changed

+333
-194
lines changed

.idea/prettier.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/content/docs/bff/fundamentals/blazor/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ builder.Services.AddBff()
6666
.AddBlazorServer();
6767

6868

69-
// ... <snip>..
69+
// ... <snip> ...
7070
7171
// Add the BFF middleware which performs anti forgery protection
7272
app.UseBff();
@@ -78,17 +78,15 @@ app.UseAntiforgery();
7878
// This has to be added after 'UseAuthorization()'
7979
app.MapBffManagementEndpoints();
8080

81-
// .. <snip>
81+
// ... <snip> ...
8282
```
8383

8484
```csharp
85-
8685
var builder = WebAssemblyHostBuilder.CreateDefault(args);
8786

8887
builder.Services
8988
.AddBffBlazorClient() // Provides auth state provider that polls the /bff/user endpoint
9089
.AddCascadingAuthenticationState();
9190

9291
builder.Services.AddLocalApiHttpClient<WeatherHttpClient>();
93-
9492
```

src/content/docs/bff/fundamentals/options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You set the options at startup time:
1919
```cs
2020
builder.Services.AddBff(options =>
2121
{
22-
// configure options here..
22+
// configure options here...
2323
})
2424
```
2525

@@ -154,7 +154,7 @@ In WASM, you configure the **BffBlazorClientOptions** using the **AddBffBlazorCl
154154
```csharp
155155
builder.Services.AddBffBlazorClient(opt =>
156156
{
157-
// configure options here..
157+
// configure options here...
158158
})
159159
```
160160

src/content/docs/bff/fundamentals/session/server-side-sessions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ Just enable session cleanup:
6969
```csharp
7070
var cn = _configuration.GetConnectionString("db");
7171

72-
builder.Services.AddBff(options => {
72+
builder.Services.AddBff(options =>
73+
{
7374
options.EnableSessionCleanup = true;
7475
})
75-
.AddEntityFrameworkServerSideSessions(options=>
76+
.AddEntityFrameworkServerSideSessions(options =>
7677
{
7778
options.UseSqlServer(cn);
7879
});

src/content/docs/bff/samples/bff-blazor.md renamed to src/content/docs/bff/samples/bff-blazor.mdx

Lines changed: 122 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ redirect_from:
1313
- /identityserver/v7/bff/samples/bff-blazor/
1414
---
1515

16+
import { Code } from "astro/components";
17+
import { Tabs, TabItem } from "@astrojs/starlight/components";
1618

1719
This quickstart walks you through how to create a BFF Blazor application. The sourcecode for this quickstart is available on [GitHub](https://github.com/DuendeSoftware/Samples/tree/main/BFF/v3/Quickstarts/BlazorBffApp)
1820

@@ -27,11 +29,11 @@ cd BlazorBffApp
2729
dotnet new blazor --interactivity auto --all-interactive
2830
```
2931

30-
This creates a blazor application with a Server project and a client project.
32+
This creates a blazor application with a Server project and a client project.
3133

3234
## Configuring the BffApp server project
3335

34-
To configure the server, the first step is to add the BFF Blazor package.
36+
To configure the server, the first step is to add the BFF Blazor package.
3537

3638
```shell
3739
cd BlazorBffApp
@@ -40,58 +42,113 @@ dotnet add package Duende.BFF.Blazor --version 3.0.0
4042

4143
Then you need to configure the application to use the BFF Blazor application. Add this to your services:
4244

43-
```csharp
44-
// BFF setup for blazor
45-
builder.Services.AddBff()
46-
.AddServerSideSessions() // Add in-memory implementation of server side sessions
47-
.AddBlazorServer();
45+
{/* prettier-ignore */}
46+
<Tabs syncKey="bffVersion">
47+
{/* prettier-ignore */}
48+
<TabItem label="Duende BFF v4">
49+
```csharp
50+
// BFF setup for blazor
51+
builder.Services.AddBff()
52+
.WithDefaultOpenIdConnectOptions(options =>
53+
{
54+
options.Authority = "https://demo.duendesoftware.com";
55+
options.ClientId = "interactive.confidential";
56+
options.ClientSecret = "secret";
57+
options.ResponseType = "code";
58+
options.ResponseMode = "query";
59+
60+
options.GetClaimsFromUserInfoEndpoint = true;
61+
options.SaveTokens = true;
62+
options.MapInboundClaims = false;
63+
64+
options.Scope.Clear();
65+
options.Scope.Add("openid");
66+
options.Scope.Add("profile");
67+
options.Scope.Add("api");
68+
options.Scope.Add("offline_access");
69+
70+
options.TokenValidationParameters.NameClaimType = "name";
71+
options.TokenValidationParameters.RoleClaimType = "role";
72+
}
73+
.WithDefaultCookieOptions(options =>
74+
{
75+
options.Cookie.Name = "__Host-blazor";
76+
77+
// Because we use an identity server that's configured on a different site
78+
// (duendesoftware.com vs localhost), we need to configure the SameSite property to Lax.
79+
// Setting it to Strict would cause the authentication cookie not to be sent after loggin in.
80+
// The user would have to refresh the page to get the cookie.
81+
// Recommendation: Set it to 'strict' if your IDP is on the same site as your BFF.
82+
options.Cookie.SameSite = SameSiteMode.Lax;
83+
})
84+
.AddServerSideSessions() // Add in-memory implementation of server side sessions
85+
.AddBlazorServer();
86+
87+
// Make sure authentication state is available to all components.
88+
builder.Services.AddCascadingAuthenticationState();
89+
90+
builder.Services.AddAuthorization();
91+
```
92+
93+
</TabItem>
94+
{/* prettier-ignore */}
95+
<TabItem label="Duende BFF v3">
96+
```csharp
97+
// BFF setup for blazor
98+
builder.Services.AddBff()
99+
.AddServerSideSessions() // Add in-memory implementation of server side sessions
100+
.AddBlazorServer();
101+
102+
// Configure the authentication
103+
builder.Services
104+
.AddAuthentication(options =>
105+
{
106+
options.DefaultScheme = "cookie";
107+
options.DefaultChallengeScheme = "oidc";
108+
options.DefaultSignOutScheme = "oidc";
109+
})
110+
.AddCookie("cookie", options =>
111+
{
112+
options.Cookie.Name = "__Host-blazor";
113+
114+
// Because we use an identity server that's configured on a different site
115+
// (duendesoftware.com vs localhost), we need to configure the SameSite property to Lax.
116+
// Setting it to Strict would cause the authentication cookie not to be sent after loggin in.
117+
// The user would have to refresh the page to get the cookie.
118+
// Recommendation: Set it to 'strict' if your IDP is on the same site as your BFF.
119+
options.Cookie.SameSite = SameSiteMode.Lax;
120+
})
121+
.AddOpenIdConnect("oidc", options =>
122+
{
123+
options.Authority = "https://demo.duendesoftware.com";
124+
options.ClientId = "interactive.confidential";
125+
options.ClientSecret = "secret";
126+
options.ResponseType = "code";
127+
options.ResponseMode = "query";
48128

129+
options.GetClaimsFromUserInfoEndpoint = true;
130+
options.SaveTokens = true;
131+
options.MapInboundClaims = false;
132+
133+
options.Scope.Clear();
134+
options.Scope.Add("openid");
135+
options.Scope.Add("profile");
136+
options.Scope.Add("api");
137+
options.Scope.Add("offline_access");
138+
139+
options.TokenValidationParameters.NameClaimType = "name";
140+
options.TokenValidationParameters.RoleClaimType = "role";
141+
});
142+
143+
// Make sure authentication state is available to all components.
144+
builder.Services.AddCascadingAuthenticationState();
145+
146+
builder.Services.AddAuthorization();
147+
148+
```
149+
</TabItem>
150+
</Tabs>
49151

50-
// Configure the authentication
51-
builder.Services.AddAuthentication(options =>
52-
{
53-
options.DefaultScheme = "cookie";
54-
options.DefaultChallengeScheme = "oidc";
55-
options.DefaultSignOutScheme = "oidc";
56-
})
57-
.AddCookie("cookie", options =>
58-
{
59-
options.Cookie.Name = "__Host-blazor";
60-
61-
// Because we use an identity server that's configured on a different site
62-
// (duendesoftware.com vs localhost), we need to configure the SameSite property to Lax.
63-
// Setting it to Strict would cause the authentication cookie not to be sent after loggin in.
64-
// The user would have to refresh the page to get the cookie.
65-
// Recommendation: Set it to 'strict' if your IDP is on the same site as your BFF.
66-
options.Cookie.SameSite = SameSiteMode.Lax;
67-
})
68-
.AddOpenIdConnect("oidc", options =>
69-
{
70-
options.Authority = "https://demo.duendesoftware.com";
71-
options.ClientId = "interactive.confidential";
72-
options.ClientSecret = "secret";
73-
options.ResponseType = "code";
74-
options.ResponseMode = "query";
75-
76-
options.GetClaimsFromUserInfoEndpoint = true;
77-
options.SaveTokens = true;
78-
options.MapInboundClaims = false;
79-
80-
options.Scope.Clear();
81-
options.Scope.Add("openid");
82-
options.Scope.Add("profile");
83-
options.Scope.Add("api");
84-
options.Scope.Add("offline_access");
85-
86-
options.TokenValidationParameters.NameClaimType = "name";
87-
options.TokenValidationParameters.RoleClaimType = "role";
88-
});
89-
90-
// Make sure authentication state is available to all components.
91-
builder.Services.AddCascadingAuthenticationState();
92-
93-
builder.Services.AddAuthorization();
94-
```
95152
To configure the web app pipeline. Replace the app.UseAntiforgery() with the code below:
96153

97154
```csharp
@@ -127,16 +184,16 @@ builder.Services
127184
.AddCascadingAuthenticationState();
128185
```
129186

130-
Your application is ready to use BFF now.
187+
Your application is ready to use BFF now.
131188

132189
## Configuring your application to use bff's features
133190

134191
Add the following components to your BlazorBffApp.Client's Component folder:
135192

136193
### LoginDisplay.razor
137194

138-
The following code shows a login / logout button depending on your state. Note, you'll need to use the
139-
logout link from the LogoutUrl claim, because this contains both the correct route and the session id.
195+
The following code shows a login / logout button depending on your state. Note, you'll need to use the
196+
logout link from the LogoutUrl claim, because this contains both the correct route and the session id.
140197
Add it to the BffBlazorApp.Client/Components folder
141198

142199
```csharp
@@ -217,7 +274,7 @@ Replace the contents of routes.razor so it matches below:
217274
</Router>
218275
```
219276

220-
This makes sure that, if you're accessing a page that requires authorization, that you are automatically redirected to Identity Server for authentication.
277+
This makes sure that, if you're accessing a page that requires authorization, that you are automatically redirected to Identity Server for authentication.
221278

222279
### Modifications to MainLayout.razor
223280

@@ -250,16 +307,16 @@ Modify your MainLayout so it matches below:
250307
</div>
251308
```
252309

253-
This adds the LoginDisplay to the header.
310+
This adds the LoginDisplay to the header.
254311

255-
Now your application supports logging in / out.
312+
Now your application supports logging in / out.
256313

257314
## Exposing APIs
258315

259-
Now we're going to expose a local api for weather forecasts to Blazor wasm and call it via a HttpClient.
316+
Now we're going to expose a local api for weather forecasts to Blazor wasm and call it via a HttpClient.
260317

261318
> By default, the system will perform both pre-rendering on the server AND WASM based rendering on the client. For this reason, you'll need to register both a server and client version of a component that retrieves data.
262-
> See the [Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#client-side-services-fail-to-resolve-during-prerendering) for more information on this.
319+
> See the [Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-9.0#client-side-services-fail-to-resolve-during-prerendering) for more information on this.
263320
264321
### Configuring the Client app
265322

@@ -280,20 +337,20 @@ public class WeatherForecast
280337
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
281338
}
282339

283-
// The IWeatherClient interface will form an abstraction between 'server' logic and client logic.
340+
// The IWeatherClient interface will form an abstraction between 'server' logic and client logic.
284341
public interface IWeatherClient
285342
{
286343
Task<WeatherForecast[]> GetWeatherForecasts();
287344
}
288345
```
289346

290-
Then register this as a component in program.cs.
347+
Then register this as a component in program.cs.
291348

292349
```csharp
293350
builder.Services
294351
.AddBffBlazorClient()// Provides auth state provider that polls the /bff/user endpoint
295352
296-
// Register a HTTP Client that's configured to fetch data from the server.
353+
// Register a HTTP Client that's configured to fetch data from the server.
297354
.AddLocalApiHttpClient<WeatherHttpClient>() ;
298355

299356
// Register the concrete implementation with the abstraction
@@ -326,7 +383,6 @@ public class ServerWeatherClient : IWeatherClient
326383
}
327384
```
328385

329-
330386
Then add an endpoint to your HTTP pipeline:
331387

332388
```csharp
@@ -341,7 +397,7 @@ builder.Services.AddSingleton<IWeatherClient, ServerWeatherClient>();
341397

342398
### Displaying Weather Information From The API
343399

344-
By default, the blazor template ships with a weather page.
400+
By default, the blazor template ships with a weather page.
345401

346402
Change the content of the **Weather.razor** like this:
347403

@@ -403,4 +459,4 @@ else
403459
forecasts = await WeatherClient.GetWeatherForecasts();
404460
}
405461
}
406-
```
462+
```

src/content/docs/bff/upgrading/bff-v3-to-v4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ services.AddBff()
6666
.WithDefaultCookieOptions(options => {
6767
// The cookie options are automatically configured with recommended practices.
6868
// However, you can change the config here.
69-
};
69+
});
7070
```
7171

7272
Adding this will automatically configure a Cookie and OpenID Connect flow.

0 commit comments

Comments
 (0)