Skip to content

Commit a361f58

Browse files
authored
Section on app settings configuration (#35378)
1 parent b217625 commit a361f58

File tree

1 file changed

+125
-1
lines changed

1 file changed

+125
-1
lines changed

aspnetcore/blazor/security/blazor-web-app-with-oidc.md

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,130 @@ In the `Program` file, all claims are serialized by setting <xref:Microsoft.AspN
10671067

10681068
:::moniker-end
10691069

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/",
1081+
"ClientId": "{CLIENT ID (BLAZOR APP)}",
1082+
"CallbackPath": "/signin-oidc",
1083+
"SignedOutCallbackPath": "/signout-callback-oidc",
1084+
"RemoteSignOutPath": "/signout-oidc",
1085+
"SignedOutRedirectUri": "/",
1086+
"Scope": [
1087+
"openid",
1088+
"profile",
1089+
"offline_access",
1090+
"{APP ID URI (WEB API)}/Weather.Get"
1091+
]
1092+
}
1093+
}
1094+
},
1095+
```
1096+
1097+
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:
1110+
1111+
```diff
1112+
- oidcOptions.Scope.Add(OpenIdConnectScope.OpenIdProfile);
1113+
- oidcOptions.Scope.Add("...");
1114+
- oidcOptions.CallbackPath = new PathString("...");
1115+
- oidcOptions.SignedOutCallbackPath = new PathString("...");
1116+
- oidcOptions.RemoteSignOutPath = new PathString("...");
1117+
- oidcOptions.Authority = "...";
1118+
- oidcOptions.ClientId = "...";
1119+
```
1120+
1121+
In the `ConfigureCookieOidc` method of `CookieOidcServiceCollectionExtensions.cs`, remove the following line:
1122+
1123+
```diff
1124+
- oidcOptions.Scope.Add(OpenIdConnectScope.OfflineAccess);
1125+
```
1126+
1127+
In the `MinimalApiJwt` project, add the following app settings configuration to the `appsettings.json` file:
1128+
1129+
```json
1130+
"Authentication": {
1131+
"Schemes": {
1132+
"Bearer": {
1133+
"Authority": "https://sts.windows.net/{TENANT ID (WEB API)}/",
1134+
"ValidAudiences": [ "{APP ID URI (WEB API)}" ]
1135+
}
1136+
}
1137+
},
1138+
```
1139+
1140+
Update the placeholders in the preceding configuration to match the values that the app uses in the `Program` file:
1141+
1142+
* `{TENANT ID (WEB API)}`: The Tenant Id of the web API.
1143+
* `{APP ID URI (WEB API)}`: The App ID URI of the web API.
1144+
1145+
Authority formats adopt the following patterns:
1146+
1147+
* ME-ID tenant type: `https://sts.windows.net/{TENANT ID}/`
1148+
* B2C tenant type: `https://login.microsoftonline.com/{TENANT ID}/v2.0/`
1149+
1150+
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`):
1151+
1152+
* ME-ID tenant type: `api://{CLIENT ID}`
1153+
* B2C tenant type: `https://{DIRECTORY NAME}.onmicrosoft.com/{CLIENT ID}`
1154+
1155+
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+
using Microsoft.IdentityModel.Validators;
1181+
```
1182+
1183+
Use the following code in the `Program` file where OIDC options are configured:
1184+
1185+
```csharp
1186+
var microsoftIssuerValidator =
1187+
AadIssuerValidator.GetAadIssuerValidator(oidcOptions.Authority);
1188+
oidcOptions.TokenValidationParameters.IssuerValidator =
1189+
microsoftIssuerValidator.Validate;
1190+
```
1191+
1192+
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+
10701194
## Redirect to the home page on logout
10711195

10721196
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
11061230

11071231
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.
11081232

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).
11101234

11111235
> [!NOTE]
11121236
> 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

Comments
 (0)