Skip to content

Commit 952c221

Browse files
authored
React to 10.0 samp updates for MAUI BH+Web App+Identity (#36944)
1 parent ef10cd7 commit 952c221

1 file changed

Lines changed: 4 additions & 51 deletions

File tree

aspnetcore/blazor/hybrid/security/maui-blazor-web-identity.md

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to build a .NET MAUI Blazor Hybrid app with a Blazor Web
55
monikerRange: '>= aspnetcore-9.0'
66
ms.author: wpickett
77
ms.custom: mvc
8-
ms.date: 11/11/2025
8+
ms.date: 04/02/2026
99
uid: blazor/hybrid/security/maui-blazor-web-identity
1010
---
1111
# .NET MAUI Blazor Hybrid and Web App with ASP.NET Core Identity
@@ -37,7 +37,7 @@ For prerequisites and preliminary steps, see <xref:blazor/hybrid/tutorials/maui>
3737
The sample app is a starter solution that contains a native, cross-platform MAUI Blazor Hybrid app, a Blazor Web App, and a Razor class library (RCL) that contains the shared UI (Razor components) used by the native and web apps.
3838

3939
1. Clone this repository or download a ZIP archive of the repository. For more information, see [How to download a sample](xref:fundamentals/index#how-to-download-a-sample).
40-
1. Make sure you have [.NET 9 and the MAUI workload installed (.NET MAUI documentation)](/dotnet/maui/get-started/installation).
40+
1. Make sure you have [the latest .NET installed with the MAUI workload](/dotnet/maui/get-started/installation).
4141
1. Open the solution in Visual Studio (2022 or later) or VS Code with the .NET MAUI extension installed.
4242
1. Set the `MauiBlazorWeb` MAUI project as the startup project. In Visual Studio, right-click the project and select **Set as Startup Project**.
4343
1. Start the `MauiBlazorWeb.Web` project without debugging. In Visual Studio, right-click on the project and select **Debug** > **Start without Debugging**.
@@ -52,7 +52,7 @@ The sample app is a starter solution that contains a native, cross-platform MAUI
5252

5353
## Shared UI
5454

55-
The shared UI is in the `MauiBlazorWeb.Shared` project. This project contains the Razor components that are shared between the MAUI and Blazor Web App projects (Home, Counter and Weather pages). The `Counter` component and `Weather` component are protected by [`[Authorize]` attributes](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute), so users can't navigate to them unless they're logged into the app.
55+
The shared UI is in the `MauiBlazorWeb.Shared` project. This project contains the Razor components that are shared between the MAUI and Blazor Web App projects (Home, Counter, and Weather pages). The `Counter` and `Weather` components are protected by [`[Authorize]` attributes](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute), so users can't navigate to them unless they're logged into the app.
5656

5757
In the [Razor directives](xref:blazor/components/index#razor-syntax) at the tops of the `Counter` component (`MauiBlazorWeb.Shared/Pages/Counter.razor`) and `Weather` component (`MauiBlazorWeb.Shared/Pages/Weather.razor`) files:
5858

@@ -147,54 +147,7 @@ private async Task LoginUser()
147147
148148
### MAUI Authentication State Provider (`MauiAuthenticationStateProvider`)
149149

150-
The `MauiAuthenticationStateProvider` class is responsible for managing the user's authentication state and providing the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationState> to the app. The `MauiAuthenticationStateProvider` class uses an <xref:System.Net.Http.HttpClient> to make requests to the server to authenticate the user. For more information, see <xref:blazor/hybrid/security/index?pivots=maui>.
151-
152-
In `MauiBlazorWeb/Services/MauiAuthenticationStateProvider.cs`:
153-
154-
```csharp
155-
private async Task<ClaimsPrincipal> LoginWithProviderAsync(LoginRequest loginModel)
156-
{
157-
var authenticatedUser = _defaultUser;
158-
LoginStatus = LoginStatus.None;
159-
160-
try
161-
{
162-
// Call the Login endpoint and pass the email and password
163-
var httpClient = HttpClientHelper.GetHttpClient();
164-
var loginData = new { loginModel.Email, loginModel.Password };
165-
using var response = await httpClient.PostAsJsonAsync(HttpClientHelper.LoginUrl,
166-
loginData);
167-
168-
LoginStatus =
169-
response.IsSuccessStatusCode ? LoginStatus.Success : LoginStatus.Failed;
170-
171-
if (LoginStatus == LoginStatus.Success)
172-
{
173-
// Save token to secure storage so the user doesn't have to login
174-
// every time
175-
var token = await response.Content.ReadAsStringAsync();
176-
_accessToken = await TokenStorage.SaveTokenToSecureStorageAsync(token,
177-
loginModel.Email);
178-
179-
authenticatedUser = CreateAuthenticatedUser(loginModel.Email);
180-
LoginStatus = LoginStatus.Success;
181-
}
182-
else
183-
{
184-
LoginFailureMessage = "Invalid Email or Password. Please try again.";
185-
LoginStatus = LoginStatus.Failed;
186-
}
187-
}
188-
catch (Exception ex)
189-
{
190-
Debug.WriteLine($"Error logging in: {ex}");
191-
LoginFailureMessage = "Server error.";
192-
LoginStatus = LoginStatus.Failed;
193-
}
194-
195-
return authenticatedUser;
196-
}
197-
```
150+
The `MauiAuthenticationStateProvider` class (`MauiBlazorWeb/Services/MauiAuthenticationStateProvider.cs`) is responsible for managing the user's authentication state and providing the <xref:Microsoft.AspNetCore.Components.Authorization.AuthenticationState> to the app. The `MauiAuthenticationStateProvider` class uses an <xref:System.Net.Http.HttpClient> to make requests to the server to authenticate the user. For more information, see <xref:blazor/hybrid/security/index?pivots=maui>.
198151

199152
The `MauiAuthenticationStateProvider` class uses the `HttpClientHelper` (`MauiBlazorWeb/Services/HttpClientHelper.cs`) to handle calling localhost via the emulators and simulators for testing. For more information on calling local services from emulators and simulators, see [Connect to local web services from Android emulators and iOS simulators (.NET MAUI documentation)](/dotnet/maui/data-cloud/local-web-services).
200153

0 commit comments

Comments
 (0)