Aspirerelease/17.0

Entra ID redirect URIs and sign-in

Это содержимое пока не доступно на вашем языке.

Web apps that sign users in need redirect URIs, also called callback URIs, registered on their Microsoft Entra ID registration. Aspire registers redirect URIs on registrations that it creates. For an existing registration, the redirect URI methods throw, so add the URIs in the Authentication settings of the Microsoft Entra admin center instead.

Aspire doesn’t discover redirect URIs on its own. The callback path is configured by the app that signs users in, so you supply it in the AppHost and it must match the app’s configuration.

Pass an endpoint and one or more callback paths so the redirect URIs follow the resource’s endpoint instead of hard-coded values.

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const entraWeb = await builder.addEntraIdApplication("entra-web");
const web = await builder.addDotnetProject("web", "../Web/Web.csproj");
await web.withExternalHttpEndpoints();
await web.withReference(entraWeb);
await entraWeb.withRedirectUris(await web.getEndpoint("https"), {
callbackPaths: ["/signin-oidc", "/signout-callback-oidc"],
});
await builder.build().run();

The scheme, host, and port come from the endpoint, and the behavior depends on the mode:

  • When you run the AppHost, Aspire waits for the endpoint to be allocated and then uses the endpoint’s finalized browser URL. This includes friendly *.dev.localhost names and URL customizations. The registration receives the real URL even when ports are assigned randomly.
  • When you publish or deploy, the URI is https:// plus the host of the resource’s compute environment, plus the callback path. The deployed endpoint must have TLS enabled. If it doesn’t, generating the infrastructure fails with an error that asks you to use an HTTPS endpoint or an explicit redirect URI. In Azure Container Apps, the endpoint must also be an external HTTP or HTTPS endpoint, for example by calling WithExternalHttpEndpoints.
  • Each callback path must start with /, and at least one path is required.

If the registered URI doesn’t match the URL that the browser uses, supply explicit redirect URIs.

When only the host name varies per environment, such as a custom domain supplied at deployment time, pass a parameter for the host and the fixed callback paths. Each URI is https://{host}{path}, so don’t include a scheme or path in the parameter value. This and the remaining examples on this page continue the previous example and use its builder and entraWeb variables.

apphost.mts
const webHost = await builder.addParameter("web-host");
await entraWeb.withRedirectUris(webHost, {
callbackPaths: ["/signin-oidc", "/signout-callback-oidc"],
});

When the externally visible URL differs from the endpoint, such as behind a gateway, reverse proxy, or custom domain, supply the full URIs. A parameter without callback paths holds comma-separated full URIs.

Aspire passes literal and parameter URIs to Microsoft Entra ID as they are, and Microsoft Entra ID enforces its redirect URI restrictions. Register only exact https:// URIs of hosts that you control, except http://localhost for local development, and don’t use wildcards. Values that you supply for a host parameter are trusted the same way, so restrict who can set them.

apphost.mts
const redirectUris = await builder.addParameter("web-redirect-uris");
await entraWeb.withRedirectUris("https://localhost:7123/signin-oidc");
await entraWeb.withRedirectUris(redirectUris);

Literal and parameter-backed URIs can be mixed and are registered in call order. In a parameter value, separate URIs with commas, without spaces. A URI can’t contain a comma. In TypeScript, call withRedirectUris repeatedly to add more than one value.

Ordinary endpoint references resolve to the allocated address, which can differ from the *.dev.localhost URL that the browser shows. For settings that must match the browser URL, such as a CORS allow list, read the endpoint’s PrimaryBrowserOrigin or BrowserOrigins property instead. Redirect URIs use the same primary browser origin.

ID-token issuance through implicit and hybrid flows is disabled by default. Enable it when your app’s OpenID Connect sign-in flow requests an ID token directly from the authorization endpoint.

apphost.mts
await entraWeb.withIdTokenIssuance();

WithIdTokenIssuance enables only ID-token issuance, not implicit access-token issuance. It isn’t needed for authorization-code-only flows. For an existing registration, the method throws, so select ID tokens (used for implicit and hybrid flows) in the registration’s Authentication settings instead.

For new production apps, prefer the authorization code flow with PKCE, and configure a client credential when the app redeems codes.

By default, only accounts in the app’s home tenant can sign in. When the registration’s Supported account types setting allows other accounts, call WithSignInAudience with the matching value.

apphost.mts
import { EntraIdSignInAudience } from './.aspire/modules/aspire.mjs';
await entraWeb.withSignInAudience(EntraIdSignInAudience.AzureADMultipleOrgs);
Sign-in audienceWho can sign inTenantId value that apps receive
AzureADMyOrg (default)Accounts in the home tenantThe home tenant ID
AzureADMultipleOrgsAccounts in any Microsoft Entra tenantorganizations
AzureADandPersonalMicrosoftAccountAccounts in any Microsoft Entra tenant and personal Microsoft accountscommon
PersonalMicrosoftAccountPersonal Microsoft accounts onlyconsumers

For the audiences other than AzureADMyOrg, apps also receive the home tenant ID as AppHomeTenantId. Microsoft.Identity.Web needs it when the app acquires tokens as itself. Always pass the home tenant ID, not organizations, common, or consumers, when you reference an existing registration.

For registrations that Aspire creates, WithSignInAudience also sets the registration’s supported account types. For existing registrations, it configures only the apps that reference the registration, so set the matching account types in the Microsoft Entra admin center.

Use WithInstance to point referencing apps at a sovereign cloud such as Azure Government. The instance must be an absolute HTTPS URL without a query string or fragment.

apphost.mts
await entraWeb.withInstance("https://login.microsoftonline.us/");

The built-in Microsoft Graph, Azure Resource Manager, and Azure Storage APIs use their global-cloud URLs. In a sovereign cloud, define the API yourself with the cloud’s resource URI and base URL, and request its permissions by ID. For an example, see Call Microsoft Graph and other first-party APIs.