Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
27 changes: 26 additions & 1 deletion aspnetcore/security/preventing-open-redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -103,5 +103,30 @@ if (RedirectHttpResult.IsLocalUrl(url))
{
return Results.LocalRedirect(url);
}
```

:::moniker-end

### Validate return URLs in Blazor apps

<xref:Microsoft.AspNetCore.Components.NavigationManager.NavigateTo*?displayProperty=nameWithType> 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 <xref:Microsoft.AspNetCore.Components.NavigationManager.Uri?displayProperty=nameWithType>, 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 <xref:Microsoft.AspNetCore.Components.NavigationManager> 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.
Loading