diff --git a/aspnetcore/blazor/security/webassembly/standalone-with-identity/qrcodes-for-authenticator-apps.md b/aspnetcore/blazor/security/webassembly/standalone-with-identity/qrcodes-for-authenticator-apps.md index 41ed1d7cd9bd..4fabe3d59bc6 100644 --- a/aspnetcore/blazor/security/webassembly/standalone-with-identity/qrcodes-for-authenticator-apps.md +++ b/aspnetcore/blazor/security/webassembly/standalone-with-identity/qrcodes-for-authenticator-apps.md @@ -2,7 +2,7 @@ title: Enable QR code generation for TOTP authenticator apps in ASP.NET Core Blazor WebAssembly with ASP.NET Core Identity description: Learn how to configure an ASP.NET Core Blazor WebAssembly app with Identity for QR code generation with TOTP authenticator apps. monikerRange: '>= aspnetcore-8.0' -ms.date: 11/11/2025 +ms.date: 11/24/2026 --- # Enable QR code generation for TOTP authenticator apps in ASP.NET Core Blazor WebAssembly with ASP.NET Core Identity @@ -524,7 +524,15 @@ Replace the `Login` component. The following version of the `Login` component: if (formResult.Succeeded && !string.IsNullOrEmpty(ReturnUrl)) { - Navigation.NavigateTo(ReturnUrl); + // ReturnUrl comes from the query string, which anyone can set in a link to the app. + // To prevent open redirects and script URLs such as "javascript:", only navigate to it if it's within the app. + // Appending "/" to the path also accepts the app's root URI without a trailing slash, such as "/app?page=2". + var baseUri = new Uri(Navigation.BaseUri); + if (Uri.TryCreate(baseUri, ReturnUrl, out var returnUri) + && (returnUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) + { + Navigation.NavigateTo(returnUri.AbsoluteUri); + } } } diff --git a/aspnetcore/security/authentication/configure-oidc-web-authentication.md b/aspnetcore/security/authentication/configure-oidc-web-authentication.md index ac500129657a..4e3f6d821bb1 100644 --- a/aspnetcore/security/authentication/configure-oidc-web-authentication.md +++ b/aspnetcore/security/authentication/configure-oidc-web-authentication.md @@ -3,7 +3,7 @@ title: Configure OpenID Connect Web (UI) authentication in ASP.NET Core ai-usage: ai-assisted description: Learn how to set up OpenID Connect authentication in an ASP.NET Core app. monikerRange: '>= aspnetcore-8.0' -ms.date: 09/18/2026 +ms.date: 11/24/2026 --- # Configure OpenID Connect Web (UI) authentication in ASP.NET Core @@ -224,29 +224,9 @@ public class LoginModel : PageModel public async Task OnGetAsync() { - var properties = GetAuthProperties(ReturnUrl); - await HttpContext.ChallengeAsync(properties); - } - - private static AuthenticationProperties GetAuthProperties(string? returnUrl) - { - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') - { - returnUrl = $"{pathBase}{returnUrl}"; - } - - return new AuthenticationProperties { RedirectUri = returnUrl }; + // ReturnUrl comes from the query string, so only redirect to local URLs to prevent open redirects. + var redirectUri = Url.IsLocalUrl(ReturnUrl) ? Url.Content(ReturnUrl) : Url.Content("~/"); + await HttpContext.ChallengeAsync(new AuthenticationProperties { RedirectUri = redirectUri }); } } diff --git a/aspnetcore/security/authentication/configure-oidc-web-authentication/samples/oidc-net8/RazorPageOidc/Pages/Login.cshtml.cs b/aspnetcore/security/authentication/configure-oidc-web-authentication/samples/oidc-net8/RazorPageOidc/Pages/Login.cshtml.cs index 88a7a0821ebd..4579ea153d9c 100644 --- a/aspnetcore/security/authentication/configure-oidc-web-authentication/samples/oidc-net8/RazorPageOidc/Pages/Login.cshtml.cs +++ b/aspnetcore/security/authentication/configure-oidc-web-authentication/samples/oidc-net8/RazorPageOidc/Pages/Login.cshtml.cs @@ -13,28 +13,8 @@ public class LoginModel : PageModel public async Task OnGetAsync() { - var properties = GetAuthProperties(ReturnUrl); - await HttpContext.ChallengeAsync(properties); - } - - private static AuthenticationProperties GetAuthProperties(string? returnUrl) - { - const string pathBase = "/"; - - // Prevent open redirects. - if (string.IsNullOrEmpty(returnUrl)) - { - returnUrl = pathBase; - } - else if (!Uri.IsWellFormedUriString(returnUrl, UriKind.Relative)) - { - returnUrl = new Uri(returnUrl, UriKind.Absolute).PathAndQuery; - } - else if (returnUrl[0] != '/') - { - returnUrl = $"{pathBase}{returnUrl}"; - } - - return new AuthenticationProperties { RedirectUri = returnUrl }; + // ReturnUrl comes from the query string, so only redirect to local URLs to prevent open redirects. + var redirectUri = Url.IsLocalUrl(ReturnUrl) ? Url.Content(ReturnUrl) : Url.Content("~/"); + await HttpContext.ChallengeAsync(new AuthenticationProperties { RedirectUri = redirectUri }); } } diff --git a/aspnetcore/security/preventing-open-redirects.md b/aspnetcore/security/preventing-open-redirects.md index e18c7a3ea3cd..eddee0e7ba14 100644 --- a/aspnetcore/security/preventing-open-redirects.md +++ b/aspnetcore/security/preventing-open-redirects.md @@ -2,7 +2,7 @@ title: Prevent open redirect attacks in ASP.NET Core ai-usage: ai-assisted description: Shows how to prevent open redirect attacks against an ASP.NET Core app -ms.date: 08/26/2026 +ms.date: 11/24/2026 --- # Prevent open redirect attacks in ASP.NET Core @@ -103,5 +103,30 @@ if (RedirectHttpResult.IsLocalUrl(url)) { return Results.LocalRedirect(url); } +``` :::moniker-end + +### Validate return URLs in Blazor apps + + doesn't check where it navigates. During static server-side rendering (static SSR), it results in a redirect to whatever URL it's given. During interactive rendering, it also follows `javascript:` URLs. Validate return URLs from the query string or form data before navigating to them or rendering them as links because Blazor doesn't block `javascript:` URLs in `href` attributes either. + +Blazor apps usually pass return URLs as absolute URLs, such as , or as paths relative to the app's base URI. `IsLocalUrl` rejects these URLs. Instead, resolve the return URL against the app's base URI and only use the result if it stays within the app. In the following example, `Navigation` is an injected and `ReturnUrl` is the return URL from the query string: + +```csharp +// The return URL comes from the query string, which anyone can set in a link to the app. +// To prevent open redirects, resolve it to an absolute URI and only use it if it's within the app. +// Appending "/" to the path also accepts the app's root URI without a trailing slash, such as "/app?page=2". +var baseUri = new Uri(Navigation.BaseUri); +if (!Uri.TryCreate(baseUri, ReturnUrl, out var redirectUri) + || !(redirectUri.GetLeftPart(UriPartial.Path) + "/").StartsWith(baseUri.AbsoluteUri, StringComparison.Ordinal)) +{ + redirectUri = baseUri; +} + +Navigation.NavigateTo(redirectUri.AbsoluteUri); +``` + +The same check works for links. To render the return URL as a link, use `redirectUri.AbsoluteUri` as the `href` value instead of calling `NavigateTo`. + +Check the resolved URI rather than the original string because string checks are easy to bypass. For example, `\/evil.example` resolves to `https://evil.example/`. Use the validated absolute URI rather than its path because the path of a URL such as `https://app.example//evil.example` is `//evil.example`, which browsers treat as a link to another host.