You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# ASP.NET Core Blazor Server additional security scenarios
13
13
14
14
By [Javier Calvarro Nelson](https://github.com/javiercn)
15
15
16
-
## Pass tokens to a Blazor Server app
16
+
::: moniker range=">= aspnetcore-5.0"
17
17
18
-
Tokens available outside of the Razor components in a Blazor Server app can be passed to components with the approach described in this section. For sample code, including a complete `Startup.ConfigureServices` example, see the [Passing tokens to a server-side Blazor application](https://github.com/javiercn/blazor-server-aad-sample).
18
+
<h2id="pass-tokens-to-a-blazor-server-app">Pass tokens to a Blazor Server app</h2>
19
+
20
+
Tokens available outside of the Razor components in a Blazor Server app can be passed to components with the approach described in this section.
19
21
20
22
Authenticate the Blazor Server app as you would with a regular Razor Pages or MVC app. Provision and save the tokens to the authentication cookie. For example:
Optionally, additional scopes are added with `options.Scope.Add("{SCOPE}");`, where the placeholder `{SCOPE}` is the additional scope to add.
40
+
41
+
Define a **scoped** token provider service that can be used within the Blazor app to resolve the tokens from [dependency injection (DI)](xref:blazor/fundamentals/dependency-injection):
42
+
43
+
```csharp
44
+
publicclassTokenProvider
45
+
{
46
+
publicstringAccessToken { get; set; }
47
+
publicstringRefreshToken { get; set; }
48
+
}
49
+
```
50
+
51
+
In `Startup.ConfigureServices`, add services for:
52
+
53
+
*`IHttpClientFactory`
54
+
*`TokenProvider`
55
+
56
+
```csharp
57
+
services.AddHttpClient();
58
+
services.AddScoped<TokenProvider>();
59
+
```
60
+
61
+
Define a class to pass in the initial app state with the access and refresh tokens:
62
+
63
+
```csharp
64
+
publicclassInitialApplicationState
65
+
{
66
+
publicstringAccessToken { get; set; }
67
+
publicstringRefreshToken { get; set; }
68
+
}
69
+
```
70
+
71
+
In the `_Host.cshtml` file, create and instance of `InitialApplicationState` and pass it as a parameter to the app:
Add a package reference to the app for the [`Microsoft.AspNet.WebApi.Client`](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client) NuGet package.
112
+
113
+
In the service that makes a secure API request, inject the token provider and retrieve the token for the API request:
<h2id="set-the-authentication-scheme">Set the authentication scheme</h2>
147
+
148
+
For an app that uses more than one Authentication Middleware and thus has more than one authentication scheme, the scheme that Blazor uses can be explicitly set in the endpoint configuration of `Startup.Configure`. The following example sets the Azure Active Directory scheme:
<h2id="pass-tokens-to-a-blazor-server-app">Pass tokens to a Blazor Server app</h2>
163
+
164
+
Tokens available outside of the Razor components in a Blazor Server app can be passed to components with the approach described in this section.
165
+
166
+
Authenticate the Blazor Server app as you would with a regular Razor Pages or MVC app. Provision and save the tokens to the authentication cookie. For example:
Optionally, additional scopes are added with `options.Scope.Add("{SCOPE}");`, where the placeholder `{SCOPE}` is the additional scope to add.
184
+
185
+
Optionally, the resource is specified with `options.Resource = "{RESOURCE}";`, where the placeholder `{RESOURCE}` is the resource. For example:
186
+
187
+
```csharp
188
+
options.Resource="https://graph.microsoft.com";
189
+
```
190
+
38
191
Define a class to pass in the initial app state with the access and refresh tokens:
39
192
40
193
```csharp
@@ -107,9 +260,9 @@ In the `App` component (`App.razor`), resolve the service and initialize it with
107
260
}
108
261
```
109
262
110
-
Add a package reference to the app for the [Microsoft.AspNet.WebApi.Client](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client) NuGet package.
263
+
Add a package reference to the app for the [`Microsoft.AspNet.WebApi.Client`](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client) NuGet package.
111
264
112
-
In the service that makes a secure API request, inject the token provider and retrieve the token to call the API:
265
+
In the service that makes a secure API request, inject the token provider and retrieve the token for the API request:
113
266
114
267
```csharp
115
268
usingSystem;
@@ -118,32 +271,31 @@ using System.Threading.Tasks;
<h2id="set-the-authentication-scheme">Set the authentication scheme</h2>
147
299
148
300
For an app that uses more than one Authentication Middleware and thus has more than one authentication scheme, the scheme that Blazor uses can be explicitly set in the endpoint configuration of `Startup.Configure`. The following example sets the Azure Active Directory scheme:
The authentication library and Blazor templates use OpenID Connect (OIDC) v1.0 endpoints. To use a v2.0 endpoint, configure the <xref:Microsoft.AspNetCore.Builder.OpenIdConnectOptions.Authority?displayProperty=nameWithType> option in the <xref:Microsoft.AspNetCore.Builder.OpenIdConnectOptions>:
312
+
In versions of ASP.NET Core prior to 5.0, the authentication library and Blazor templates use OpenID Connect (OIDC) v1.0 endpoints. To use a v2.0 endpoint with versions of ASP.NET Core prior to 5.0, configure the <xref:Microsoft.AspNetCore.Builder.OpenIdConnectOptions.Authority?displayProperty=nameWithType> option in the <xref:Microsoft.AspNetCore.Builder.OpenIdConnectOptions>:
Blazor Server apps are configured for security in the same manner as ASP.NET Core apps. For more information, see the articles under <xref:security/index>. Topics under this overview apply specifically to Blazor Server.
16
+
Blazor Server apps are configured for security in the same manner as ASP.NET Core apps. For more information, see the articles under <xref:security/index>. Topics under this overview apply specifically to Blazor Server.
17
17
18
18
## Blazor Server project template
19
19
@@ -92,7 +92,14 @@ Using the `-o|--output` option, the command uses the value provided for the `{AP
92
92
* Create a folder for the project.
93
93
* Name the project.
94
94
95
-
For more information, see the [`dotnet new`](/dotnet/core/tools/dotnet-new) command in the .NET Core Guide.
95
+
For more information:
96
+
97
+
* See the [`dotnet new`](/dotnet/core/tools/dotnet-new) command in the .NET Core Guide.
98
+
* Execute the help command for the Blazor Server template (`blazorserver`) in a command shell:
99
+
100
+
```dotnetcli
101
+
dotnet new blazorserver --help
102
+
```
96
103
97
104
---
98
105
@@ -102,3 +109,8 @@ Scaffold Identity into a Blazor Server project:
0 commit comments