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
Copy file name to clipboardExpand all lines: aspnetcore/security/blazor/server.md
+192-1Lines changed: 192 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Learn how to mitigate security threats to Blazor Server apps.
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: riande
7
7
ms.custom: mvc
8
-
ms.date: 03/16/2020
8
+
ms.date: 03/31/2020
9
9
no-loc: [Blazor, SignalR]
10
10
uid: security/blazor/server
11
11
---
@@ -24,6 +24,197 @@ In constrained environments, such as inside corporate networks or intranets, som
24
24
* Doesn't apply in the constrained environment.
25
25
* Isn't worth the cost to implement because the security risk is low in a constrained environment.
26
26
27
+
## Blazor Server project template
28
+
29
+
The Blazor Server project template can be configured for authentication when the project is created.
30
+
31
+
# [Visual Studio](#tab/visual-studio)
32
+
33
+
Follow the Visual Studio guidance in the <xref:blazor/get-started> article to create a new Blazor Server project with an authentication mechanism.
34
+
35
+
After choosing the **Blazor Server App** template in the **Create a new ASP.NET Core Web Application** dialog, select **Change** under **Authentication**.
36
+
37
+
A dialog opens to offer the same set of authentication mechanisms available for other ASP.NET Core projects:
38
+
39
+
***No Authentication**
40
+
***Individual User Accounts**– User accounts can be stored:
41
+
* Within the app using ASP.NET Core's [Identity](xref:security/authentication/identity) system.
42
+
* With [Azure AD B2C](xref:security/authentication/azure-ad-b2c).
43
+
***Work or School Accounts**
44
+
***Windows Authentication**
45
+
46
+
# [Visual Studio Code](#tab/visual-studio-code)
47
+
48
+
Follow the Visual Studio Code guidance in the <xref:blazor/get-started> article to create a new Blazor Server project with an authentication mechanism:
49
+
50
+
```dotnetcli
51
+
dotnet new blazorserver -o {APP NAME} -au {AUTHENTICATION}
52
+
```
53
+
54
+
Permissible authentication values (`{AUTHENTICATION}`) are shown in the following table.
55
+
56
+
| Authentication mechanism |`{AUTHENTICATION}` value |
| Individual<br>Users stored in the app with ASP.NET Core Identity. |`Individual`|
60
+
| Individual<br>Users stored in [Azure AD B2C](xref:security/authentication/azure-ad-b2c). |`IndividualB2C`|
61
+
| Work or School Accounts<br>Organizational authentication for a single tenant. |`SingleOrg`|
62
+
| Work or School Accounts<br>Organizational authentication for multiple tenants. |`MultiOrg`|
63
+
| Windows Authentication |`Windows`|
64
+
65
+
The command creates a folder named with the value provided for the `{APP NAME}` placeholder and uses the folder name as the app's name. For more information, see the [dotnet new](/dotnet/core/tools/dotnet-new) command in the .NET Core Guide.
66
+
67
+
# [Visual Studio for Mac](#tab/visual-studio-mac)
68
+
69
+
1. Follow the Visual Studio for Mac guidance in the <xref:blazor/get-started> article.
70
+
71
+
1. On the **Configure your new Blazor Server App** step, select **Individual Authentication (in-app)** from the **Authentication** drop down.
72
+
73
+
1. The app is created for individual users stored in the app with ASP.NET Core Identity.
74
+
75
+
# [.NET Core CLI](#tab/netcore-cli/)
76
+
77
+
Follow the .NET Core CLI guidance in the <xref:blazor/get-started> article to create a new Blazor Server project with an authentication mechanism:
78
+
79
+
```dotnetcli
80
+
dotnet new blazorserver -o {APP NAME} -au {AUTHENTICATION}
81
+
```
82
+
83
+
Permissible authentication values (`{AUTHENTICATION}`) are shown in the following table.
84
+
85
+
| Authentication mechanism |`{AUTHENTICATION}` value |
| Individual<br>Users stored in the app with ASP.NET Core Identity. |`Individual`|
89
+
| Individual<br>Users stored in [Azure AD B2C](xref:security/authentication/azure-ad-b2c). |`IndividualB2C`|
90
+
| Work or School Accounts<br>Organizational authentication for a single tenant. |`SingleOrg`|
91
+
| Work or School Accounts<br>Organizational authentication for multiple tenants. |`MultiOrg`|
92
+
| Windows Authentication |`Windows`|
93
+
94
+
The command creates a folder named with the value provided for the `{APP NAME}` placeholder and uses the folder name as the app's name. For more information, see the [dotnet new](/dotnet/core/tools/dotnet-new) command in the .NET Core Guide.
95
+
96
+
---
97
+
98
+
## Pass tokens to a Blazor Server app
99
+
100
+
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:
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).
115
+
116
+
Define a class to pass in the initial app state with the access and refresh tokens:
117
+
118
+
```csharp
119
+
publicclassInitialApplicationState
120
+
{
121
+
publicstringAccessToken { get; set; }
122
+
publicstringRefreshToken { get; set; }
123
+
}
124
+
```
125
+
126
+
Define a **scoped** token provider service that can be used within the Blazor app to resolve the tokens from DI:
127
+
128
+
```csharp
129
+
usingSystem;
130
+
usingSystem.Security.Claims;
131
+
usingSystem.Threading.Tasks;
132
+
133
+
publicclassTokenProvider
134
+
{
135
+
publicstringAccessToken { get; set; }
136
+
publicstringRefreshToken { get; set; }
137
+
}
138
+
```
139
+
140
+
In `Startup.ConfigureServices`, register the token provider service:
141
+
142
+
```csharp
143
+
services.AddScoped<TokenProvider>();
144
+
```
145
+
146
+
In the *_Host.cshtml* file, create and instance of `InitialApplicationState` and pass it as a parameter to the app:
Resource exhaustion can occur when a client interacts with the server and causes the server to consume excessive resources. Excessive resource consumption primarily affects:
Most apps only require an access token to interact with the protected resources that they use. In some scenarios, an app might require more than one token in order to interact with two or more resources.
23
+
24
+
In the following example, additional Azure Active Directory (AAD) Microsoft Graph API scopes are required by an app to read user data and send mail. After adding the Microsoft Graph API permissions in the Azure AAD portal, the additional scopes are configured in the Client app (`Program.Main`, *Program.cs*):
The `IAccessTokenProvider.RequestToken` method provides an overload that allows an app to provision a token with a given set of scopes, asseeninthefollowingexample:
@@ -155,19 +194,6 @@ During an authentication operation, there are cases where you want to save the a
155
194
}
156
195
```
157
196
158
-
## Request additional access tokens
159
-
160
-
Most apps only require an access token to interact with the protected resources that they use. In some scenarios, an app might require more than one token in order to interact with two or more resources. The `IAccessTokenProvider.RequestToken` method provides an overload that allows an app to provision a token with a given set of scopes, as seen in the following example:
Bydefault, the `Microsoft.AspNetCore.Components.WebAssembly.Authentication` libraryusestheroutesshowninthefollowingtableforrepresentingdifferentauthenticationstates.
Run the app from the Server project. When using Visual Studio, select the Server project in **Solution Explorer** and select the **Run** button in the toolbar or start the app from the **Debug** menu.
0 commit comments