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/blazor/security/blazor-web-app-with-oidc.md
+125-1Lines changed: 125 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1067,6 +1067,130 @@ In the `Program` file, all claims are serialized by setting <xref:Microsoft.AspN
1067
1067
1068
1068
:::moniker-end
1069
1069
1070
+
## Supply configuration with the JSON configuration provider (app settings)
1071
+
1072
+
The [sample solution projects](#sample-solution) configure OIDC and JWT bearer authentication in their `Program` files in order to make configuration settings discoverable using C# autocompletion. Professional apps usually use a *configuration provider* to configure OIDC options, such as the default [JSON configuration provider](xref:fundamentals/configuration/index). The JSON configuration provider loads configuration from app settings files `appsettings.json`/`appsettings.{ENVIRONMENT}.json`, where the `{ENVIRONMENT}` placeholder is the app's [runtime environment](xref:fundamentals/environments). Follow the guidance in this section to use app settings files for configuration.
1073
+
1074
+
In the app settings file (`appsettings.json`) of the `BlazorWebAppOidc`, `BlazorWebAppOidcServer`, or `BlazorWebAppOidcBff` project, add the following JSON configuration:
1075
+
1076
+
```json
1077
+
"Authentication": {
1078
+
"Schemes": {
1079
+
"MicrosoftOidc": {
1080
+
"Authority": "https://login.microsoftonline.com/{TENANT ID (BLAZOR APP)}/v2.0/",
Update the placeholders in the preceding configuration to match the values that the app uses in the `Program` file:
1098
+
1099
+
*`{TENANT ID (BLAZOR APP)}`: The Tenant Id of the Blazor app.
1100
+
*`{CLIENT ID (BLAZOR APP)}`: The Client Id of the Blazor app.
1101
+
*`{APP ID URI (WEB API)}`: The App ID URI of the web API.
1102
+
1103
+
The "common" Authority (`https://login.microsoftonline.com/common/v2.0/`) should be used for multi-tenant apps. To use the "common" Authority for single-tenant apps, see the [Use the "common" Authority for single-tenant apps](#use-the-common-authority-for-single-tenant-apps) section.
1104
+
1105
+
Update any other values in the preceding configuration to match custom/non-default values used in the `Program` file.
1106
+
1107
+
The configuration is automatically picked up by the authentication builder.
1108
+
1109
+
Remove the following lines from the `Program` file:
Audience formats adopt the following patterns (`{CLIENT ID}` is the Client Id of the web API; `{DIRECTORY NAME}` is the directory name, for example, `contoso`):
The configuration is automatically picked up by the JWT bearer authentication builder.
1156
+
1157
+
Remove the following lines from the `Program` file:
1158
+
1159
+
```diff
1160
+
- jwtOptions.Authority = "...";
1161
+
- jwtOptions.Audience = "...";
1162
+
```
1163
+
1164
+
For more information on configuration, see the following resources:
1165
+
1166
+
*<xref:fundamentals/configuration/index>
1167
+
*<xref:blazor/fundamentals/configuration>
1168
+
1169
+
## Use the "common" Authority for single-tenant apps
1170
+
1171
+
You can use the "common" Authority for single-tenant apps, but you must take the following steps to implement a custom issuer validator.
1172
+
1173
+
Add the [`Microsoft.IdentityModel.Validators` NuGet package](https://www.nuget.org/packages/Microsoft.IdentityModel.Validators) to the `BlazorWebAppOidc`, `BlazorWebAppOidcServer`, or `BlazorWebAppOidcBff` project.
1174
+
1175
+
[!INCLUDE[](~/includes/package-reference.md)]
1176
+
1177
+
At the top of the `Program` file, make the <xref:Microsoft.IdentityModel.Validators?displayProperty=fullName> namespace available:
1178
+
1179
+
```csharp
1180
+
usingMicrosoft.IdentityModel.Validators;
1181
+
```
1182
+
1183
+
Use the following code in the `Program` file where OIDC options are configured:
For more information, see [SecurityTokenInvalidIssuerException with OpenID Connect and the Azure AD "common" endpoint (`AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet`#1731)](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/1731).
1193
+
1070
1194
## Redirect to the home page on logout
1071
1195
1072
1196
The `LogInOrOut` component (`Layout/LogInOrOut.razor`) sets a hidden field for the return URL (`ReturnUrl`) to the current URL (`currentURL`). When the user signs out of the app, the identity provider returns the user to the page from which they logged out. If the user logs out from a secure page, they're returned to the same secure page and sent back through the authentication process. This authentication flow is reasonable when users need to change accounts regularly.
@@ -1106,7 +1230,7 @@ Alternatively, use the following `LogInOrOut` component, which doesn't supply a
1106
1230
1107
1231
The custom cookie refresher (`CookieOidcRefresher.cs`) implementation updates the user's claims automatically when they expire. The current implementation expects to receive an ID token from the token endpoint in exchange for the refresh token. The claims in this ID token are then used to overwrite the user's claims.
1108
1232
1109
-
The sample implementation doesn't include code for requesting claims from the [UserInfo endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo) on token refresh. For more information, see [`BlazorWebAppOidc AddOpenIdConnect with GetClaimsFromUserInfoEndpoint = true doesn't propogate role claims to client` (`dotnet/aspnetcore`#58826)](https://github.com/dotnet/aspnetcore/issues/58826#issuecomment-2492738142).
1233
+
The sample implementation doesn't include code for requesting claims from the [UserInfo endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo) on token refresh. For more information, see [`BlazorWebAppOidc AddOpenIdConnect with GetClaimsFromUserInfoEndpoint = true doesn't propogate [sic] role claims to client` (`dotnet/aspnetcore`#58826)](https://github.com/dotnet/aspnetcore/issues/58826#issuecomment-2492738142).
1110
1234
1111
1235
> [!NOTE]
1112
1236
> Some identity providers [only return an access token when using a refresh token](https://openid.net/specs/openid-connect-core-1_0.html#RefreshTokenResponse). The `CookieOidcRefresher` can be updated with additional logic to continue to use the prior set of claims stored in the authentication cookie or use the access token to request claims from the UserInfo endpoint.
0 commit comments