Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Standardmäßig laufen Microsoft Entra-ID-Token (ID-Token, Zugriffstoken und SAML-Token) nach einer Stunde ab. Auch standardmäßig legen ASP.NET und ASP.NET Core Middleware ihre Authentifizierungstickets auf den Ablauf dieser Token fest. Wenn Sie nicht möchten, dass Ihre Webanwendung Benutzer zur Microsoft Entra-ID umleitet, damit sie sich erneut anmelden, können Sie das Middleware-Authentifizierungsticket anpassen.
Diese Anpassung kann auch bei der Behebung von AJAX-bezogenen Problemen helfen, z. B. CORS-Fehlern (Cross-Origin Resource Sharing), die bei login.microsoftonline.com auftreten. Diese Probleme treten häufig auf, wenn Ihre App sowohl als Webanwendung als auch als Web-API fungiert.
Für ASP.NET
Aktualisieren Sie die app.UseCookieAuthentication()
Methode in der ConfigureAuth
Methode der Startup.Auth.cs
Datei zu:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(),
Provider = new CookieAuthenticationProvider()
{
OnResponseSignIn = (context) =>
{
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
}
}
});
Decoupieren Sie dann die Tokenlebensdauer von der Web-App:
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
UseTokenLifetime = false,
...
Für ASP.NET Core
In ASP.NET Core müssen Sie das OnTokenValidated
Ereignis hinzufügen, um die Ticketeigenschaften zu aktualisieren. Durch diese Ergänzung wird festgelegt, dass die Ablaufzeit des Tickets vor der Umleitung der Anwendung zur erneuten Authentifizierung an die Microsoft Entra-ID erfolgt.
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
// decouple the token lifetime from the Web App
options.UseTokenLifetime = false;
// other configurations...
// ...
var onTokenValidated = options.Events.OnTokenValidated;
options.Events ??= new OpenIdConnectEvents();
options.Events.OnTokenValidated = async context =>
{
await onTokenValidated(context);
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
};
});
Beispiele
Hier sind einige Beispiele für die Vorgehensweise zum Erstellen dieser Einstellung:
Wenn Sie Code verwenden, der dem folgenden Beispiel ähnelt, um die Microsoft Entra ID-Authentifizierung hinzuzufügen:
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options))
Fügen Sie dann den folgenden Code hinzu:
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
// decouple the id_token lifetime from the Web App
options.UseTokenLifetime = false;
//…
var onTokenValidated = options.Events.OnTokenValidated;
options.Events ??= new OpenIdConnectEvents();
options.Events.OnTokenValidated = async context =>
{
await onTokenValidated(context);
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
};
});
Ihre Konfiguration in Startup.cs sollte dem folgenden Beispiel ähneln:
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options))
.AddCookie();
//...
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
// decouple the token lifetime from the Web App
options.UseTokenLifetime = false;
//...
var onTokenValidated = options.Events.OnTokenValidated;
options.Events ??= new OpenIdConnectEvents();
options.Events.OnTokenValidated = async context =>
{
await onTokenValidated(context);
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
};
});
//...
}
Wenn Sie Microsoft.Identity.Web
verwenden, um Ihre Microsoft Entra ID-Konfiguration hinzuzufügen:
//…
using Microsoft.Identity.Web;
//…
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(options =>
{
Configuration.Bind("AzureAD", options);
// decouple the token lifetime from the Web App
options.UseTokenLifetime = false;
var onTokenValidated = options.Events.OnTokenValidated;
options.Events ??= new OpenIdConnectEvents();
options.Events.OnTokenValidated = async context =>
{
await onTokenValidated(context);
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
};
});
// ...
}
Wenn Sie eine benutzerdefinierte OpenIdConnectOptions
implementieren, um die Microsoft Entra ID-Authentifizierung zu konfigurieren:
services.Configure<OpenIdConnectOptions>(options =>
{
//…
options.Events.OnTokenValidated = async context =>
{
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
};
});
Wenn Sie eine ASP.NET Core WS-Fed-Anwendung integrieren:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(WsFederationDefaults.AuthenticationScheme)
.AddCookie()
.AddWsFederation(options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// decouple the token lifetime from the Web App
options.UseTokenLifetime = false;
// MetadataAddress for your Azure Active Directory instance.
options.MetadataAddress = "https://login.microsoftonline.com/common/federationmetadata/2007-06/federationmetadata.xml";
// Wtrealm is the app's identifier in the Active Directory instance.
// For ADFS, use the relying party's identifier, its WS-Federation Passive protocol URL:
options.Wtrealm = "https://localhost:44307/";
// For AAD, use the Application ID URI from the app registration's Overview blade:
options.Wtrealm = "https://contoso.onmicrosoft.com/wsfedapp";
// Set the Authentication Ticket to expire at a custom time
var onTokenValidated = options.Events.OnTokenValidated;
options.Events ??= new OpenIdConnectEvents();
options.Events.OnSecurityTokenValidated = async context =>
{
context.Properties.ExpiresUtc = DateTimeOffset.UtcNow.AddHours(12);
}
Mehr Informationen
Diese Einstellungen steuern den Ablauf des Authentifizierungstickets, das bestimmt, wie lange ein Benutzer angemeldet bleibt. Sie können dieses Ablaufdatum entsprechend Ihrer Anforderungen konfigurieren.
Hinweis
Wenn Sie den Ablauf des Tickets ändern, haben Benutzer möglicherweise weiterhin Zugriff auf Ihre Anwendung, auch wenn sie in der Microsoft Entra-ID gelöscht oder deaktiviert sind. Diese Bedingung bleibt gültig, bis das Ticket abläuft.
Kontaktieren Sie uns für Hilfe
Wenn Sie Fragen haben oder Hilfe benötigen, erstellen Sie eine Support-Anfrage oder wenden Sie sich an den Azure Community-Support. Sie können auch Produktfeedback an die Azure Feedback Community senden.