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 @@ -11,10 +11,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="10.0.14" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ else
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th aria-label="Temperature in Celsius">Temp. (C)</th>
<th aria-label="Temperature in Fahrenheit">Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inherits LayoutComponentBase
@inherits LayoutComponentBase

<div class="page">
<div class="sidebar">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="module" src="@Assets["Components/Layout/ReconnectModal.razor.js"]"></script>
<script type="module" src="@Assets["Layout/ReconnectModal.razor.js"]"></script>

<dialog id="components-reconnect-modal" data-nosnippet>
<div class="components-reconnect-container">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="10.0.14" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiBlazorWeb.Shared\MauiBlazorWeb.Shared.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions 10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MauiBlazorWeb.Shared.Services;
using MauiBlazorWeb.Web.Client.Services;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.Services.AddAuthorizationCore();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddAuthenticationStateDeserialization();

builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddSingleton<IFormFactor, FormFactor>();
builder.Services.AddScoped<IWeatherService, WeatherService>();

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inject NavigationManager NavigationManager
@inject NavigationManager NavigationManager

@code {
protected override void OnInitialized()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
@using MauiBlazorWeb.Web.Components.Account.Shared

<Router AppAssembly="typeof(Program).Assembly"
AdditionalAssemblies="new[] { typeof(Shared._Imports).Assembly }"
AdditionalAssemblies="new[] { typeof(MauiBlazorWeb.Shared._Imports).Assembly }"
NotFoundPage="typeof(Pages.NotFound)">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MauiBlazorWeb.Shared.Services;

namespace MauiBlazorWeb.Web.Client.Services;

public sealed class FormFactor : IFormFactor
{
public string GetFormFactor() => "Web";

public string GetPlatform() => "WebAssembly";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Net.Http.Json;
using MauiBlazorWeb.Shared.Services;

namespace MauiBlazorWeb.Web.Client.Services;

public sealed class WeatherService(HttpClient httpClient) : IWeatherService
{
public async Task<WeatherForecast[]> GetWeatherForecastsAsync()
{
return await httpClient.GetFromJsonAsync<WeatherForecast[]>("api/weather")
?? throw new InvalidOperationException("The weather API returned no forecasts.");
}
}
12 changes: 12 additions & 0 deletions 10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web.Client/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using MauiBlazorWeb.Shared
@using MauiBlazorWeb.Web.Client
@using MauiBlazorWeb.Web.Client.Layout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@inherits LayoutComponentBase
@layout MauiBlazorWeb.Web.Components.Layout.MainLayout
@layout MauiBlazorWeb.Web.Client.Layout.MainLayout

<h1>Manage your account</h1>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
private HttpContext HttpContext { get; set; } = default!;

private IComponentRenderMode? PageRenderMode =>
HttpContext.AcceptsInteractiveRouting() ? InteractiveServer : null;
HttpContext.AcceptsInteractiveRouting() ? InteractiveAuto : null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using MauiBlazorWeb.Shared
@using MauiBlazorWeb.Web.Client
@using MauiBlazorWeb.Web.Client.Layout
@using MauiBlazorWeb.Web
@using MauiBlazorWeb.Web.Components
@using MauiBlazorWeb.Web.Components.Layout
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>
<UserSecretsId>cc1a9695-3349-4242-95b1-b761a0b3bd4c</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.14" PrivateAssets="all" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.14" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.14" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MauiBlazorWeb.Web.Client\MauiBlazorWeb.Web.Client.csproj" />
<ProjectReference Include="..\MauiBlazorWeb.Shared\MauiBlazorWeb.Shared.csproj" />
</ItemGroup>

Expand Down
33 changes: 23 additions & 10 deletions 10.0/MauiBlazorWebIdentity/MauiBlazorWeb.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
using MauiBlazorWeb.Shared.Services;
using MauiBlazorWeb.Web.Client;
using MauiBlazorWeb.Web.Components;
using MauiBlazorWeb.Web.Components.Account;
using MauiBlazorWeb.Web.Data;
using MauiBlazorWeb.Web.Services;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents()
.AddAuthenticationStateSerialization();

// Add device-specific services used by the MauiBlazorWeb.Shared project
builder.Services.AddSingleton<IFormFactor, FormFactor>();
Expand All @@ -21,12 +25,13 @@
builder.Services.AddScoped<IdentityRedirectManager>();
builder.Services.AddScoped<AuthenticationStateProvider, IdentityRevalidatingAuthenticationStateProvider>();

// Ensure unauthenticated web clients redirect to login rather than receive 401.
// Only DefaultChallengeScheme is set here; AddIdentityApiEndpoints sets DefaultScheme
// to BearerAndApplicationScheme which handles both bearer tokens (MAUI) and cookies (web).
// Ensure unauthenticated page requests redirect to login rather than receive 401.
// Keep the template's external sign-in cookie. AddIdentityApiEndpoints sets DefaultScheme
// to BearerAndApplicationScheme for bearer tokens (MAUI) and cookies (web).
builder.Services.AddAuthentication(options =>
{
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
});

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
Expand Down Expand Up @@ -54,6 +59,7 @@
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
// Apply migrations & create database if needed at startup
using (var scope = app.Services.CreateScope())
{
Expand All @@ -78,18 +84,25 @@
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddAdditionalAssemblies(typeof(MauiBlazorWeb.Shared._Imports).Assembly);
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(MauiBlazorWeb.Web.Client._Imports).Assembly,
typeof(MauiBlazorWeb.Shared._Imports).Assembly);

// Needed for external clients to log in
app.MapGroup("/identity").MapIdentityApi<ApplicationUser>();
app.MapGroup("/identity")
.MapIdentityApi<ApplicationUser>()
.WithMetadata(new SkipStatusCodePagesAttribute());

// Needed for Identity Blazor components
app.MapAdditionalIdentityEndpoints();

// Add the weather API endpoint and require authorization
app.MapGet("/api/weather", async (IWeatherService weatherService) =>
{
var forecasts = await weatherService.GetWeatherForecastsAsync();
return Results.Ok(forecasts);
}).RequireAuthorization();
{
var forecasts = await weatherService.GetWeatherForecastsAsync();
return TypedResults.Ok(forecasts);
})
.RequireAuthorization()
.WithMetadata(new SkipStatusCodePagesAttribute());

app.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7157;http://localhost:5101",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -22,6 +23,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5101",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -30,6 +32,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
6 changes: 6 additions & 0 deletions 10.0/MauiBlazorWebIdentity/MauiBlazorWeb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiBlazorWeb.Shared", "Mau
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiBlazorWeb.Web", "MauiBlazorWeb.Web\MauiBlazorWeb.Web.csproj", "{602A0A61-85A3-4373-898C-FBE285A5D09A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MauiBlazorWeb.Web.Client", "MauiBlazorWeb.Web.Client\MauiBlazorWeb.Web.Client.csproj", "{56C2A82B-1CD8-4CF0-BCCE-23CA2988EAC7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{602A0A61-85A3-4373-898C-FBE285A5D09A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{602A0A61-85A3-4373-898C-FBE285A5D09A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{602A0A61-85A3-4373-898C-FBE285A5D09A}.Release|Any CPU.Build.0 = Release|Any CPU
{56C2A82B-1CD8-4CF0-BCCE-23CA2988EAC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56C2A82B-1CD8-4CF0-BCCE-23CA2988EAC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56C2A82B-1CD8-4CF0-BCCE-23CA2988EAC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56C2A82B-1CD8-4CF0-BCCE-23CA2988EAC7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span>Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="register">
<span class="bi bi-person-badge-nav-menu" aria-hidden="true"></span>Register
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="login">
<span class="bi bi-person-badge-nav-menu" aria-hidden="true"></span>Log In
Expand Down
Loading