Skip to content

Commit 809406d

Browse files
committed
Added .NET 10 version of the MAUI sample
1 parent fb145a3 commit 809406d

38 files changed

+1189
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiApp1", "MauiApp1\MauiApp1.csproj", "{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{F47CF5D7-9D19-48C1-9AC3-0F26B4133D92}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {CF4F4884-6B6D-4CA0-9BD0-FCD86EFD26F4}
26+
EndGlobalSection
27+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:MauiApp1"
5+
x:Class="MauiApp1.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
namespace MauiApp1;
3+
4+
public partial class App : Application
5+
{
6+
public App() => InitializeComponent();
7+
8+
protected override Window CreateWindow(IActivationState? activationState) => new Window(new AppShell());
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="MauiApp1.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:MauiApp1"
7+
Shell.FlyoutBehavior="Disabled"
8+
Title="MauiApp1">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MauiApp1;
2+
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="MauiApp1.MainPage">
5+
6+
<ScrollView>
7+
<VerticalStackLayout
8+
Padding="30,0"
9+
Spacing="25">
10+
11+
<Label
12+
Text="OIDC Client Sample"
13+
Style="{StaticResource Headline}"
14+
SemanticProperties.HeadingLevel="Level1" />
15+
16+
<Button
17+
x:Name="LoginButton"
18+
Text="Login"
19+
Clicked="OnLoginClicked"
20+
HorizontalOptions="Fill" />
21+
<Button
22+
x:Name="ApiButton"
23+
Text="Call API"
24+
Clicked="OnApiClicked"
25+
HorizontalOptions="Fill" />
26+
27+
<Editor x:Name="editor"
28+
HeightRequest="350" />
29+
30+
</VerticalStackLayout>
31+
</ScrollView>
32+
33+
</ContentPage>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Duende.IdentityModel.Client;
2+
using Duende.IdentityModel.OidcClient;
3+
using System.Text;
4+
using System.Text.Json;
5+
6+
namespace MauiApp1;
7+
8+
public partial class MainPage : ContentPage
9+
{
10+
private readonly OidcClient _client = default!;
11+
private string? _currentAccessToken;
12+
13+
public MainPage(OidcClient client)
14+
{
15+
InitializeComponent();
16+
17+
_client = client;
18+
}
19+
20+
private async void OnLoginClicked(object sender, EventArgs e)
21+
{
22+
editor.Text = "Login Clicked";
23+
24+
var result = await _client.LoginAsync();
25+
26+
if (result.IsError)
27+
{
28+
editor.Text = result.Error;
29+
return;
30+
}
31+
32+
_currentAccessToken = result.AccessToken;
33+
34+
var sb = new StringBuilder(128);
35+
36+
sb.AppendLine("claims:");
37+
foreach (var claim in result.User.Claims)
38+
{
39+
sb.AppendLine($"{claim.Type}: {claim.Value}");
40+
}
41+
42+
sb.AppendLine();
43+
sb.AppendLine("access token:");
44+
sb.AppendLine(result.AccessToken);
45+
46+
if (!string.IsNullOrWhiteSpace(result.RefreshToken))
47+
{
48+
sb.AppendLine();
49+
sb.AppendLine("access token:");
50+
sb.AppendLine(result.AccessToken);
51+
}
52+
53+
editor.Text = sb.ToString();
54+
}
55+
56+
private async void OnApiClicked(object sender, EventArgs e)
57+
{
58+
editor.Text = "API Clicked";
59+
60+
if (_currentAccessToken != null)
61+
{
62+
var client = new HttpClient();
63+
client.SetBearerToken(_currentAccessToken);
64+
65+
var response = await client.GetAsync("https://demo.duendesoftware.com/api/test");
66+
if (response.IsSuccessStatusCode)
67+
{
68+
var content = await response.Content.ReadAsStringAsync();
69+
var doc = JsonDocument.Parse(content).RootElement;
70+
editor.Text = JsonSerializer.Serialize(doc, new JsonSerializerOptions { WriteIndented = true });
71+
}
72+
else
73+
{
74+
editor.Text = response.ReasonPhrase;
75+
}
76+
}
77+
}
78+
}
79+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net10.0-android;net10.0-ios;net10.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net10.0-windows10.0.19041.0</TargetFrameworks>
6+
7+
<!-- Note for MacCatalyst:
8+
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
9+
When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
10+
The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
11+
either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
12+
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->
13+
14+
<OutputType>Exe</OutputType>
15+
<RootNamespace>MauiApp1</RootNamespace>
16+
<UseMaui>true</UseMaui>
17+
<SingleProject>true</SingleProject>
18+
<ImplicitUsings>enable</ImplicitUsings>
19+
<Nullable>enable</Nullable>
20+
<!-- https://github.com/CommunityToolkit/Maui/issues/2921 -->
21+
<NoWarn>NU1608</NoWarn>
22+
<MauiEnableXamlCBindingWithSourceCompilation>true</MauiEnableXamlCBindingWithSourceCompilation>
23+
24+
<!-- Display name -->
25+
<ApplicationTitle>MauiApp1</ApplicationTitle>
26+
27+
<!-- App Identifier -->
28+
<ApplicationId>com.companyname.mauiapp1</ApplicationId>
29+
30+
<!-- Versions -->
31+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
32+
<ApplicationVersion>1</ApplicationVersion>
33+
34+
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
35+
<WindowsPackageType>None</WindowsPackageType>
36+
37+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
38+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
39+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
40+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
41+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
42+
</PropertyGroup>
43+
44+
<ItemGroup>
45+
<!-- App Icon -->
46+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
47+
48+
<!-- Splash Screen -->
49+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
50+
51+
<!-- Images -->
52+
<MauiImage Include="Resources\Images\*" />
53+
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
54+
55+
<!-- Custom Fonts -->
56+
<MauiFont Include="Resources\Fonts\*" />
57+
58+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
59+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
60+
</ItemGroup>
61+
62+
<ItemGroup>
63+
<PackageReference Include="Duende.IdentityModel.OidcClient" Version="7.0.0-rc.1" />
64+
<PackageReference Include="Microsoft.Maui.Controls" Version="10.0.10" />
65+
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="10.0.10" />
66+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.0" />
67+
</ItemGroup>
68+
69+
</Project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Duende.IdentityModel.Client;
2+
using Duende.IdentityModel.OidcClient.Browser;
3+
4+
namespace MauiApp1;
5+
6+
public class MauiAuthenticationBrowser : Duende.IdentityModel.OidcClient.Browser.IBrowser
7+
{
8+
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
9+
{
10+
try
11+
{
12+
var result = await WebAuthenticator.Default.AuthenticateAsync(
13+
new Uri(options.StartUrl),
14+
new Uri(options.EndUrl),
15+
cancellationToken);
16+
17+
var url = new RequestUrl("myapp://callback")
18+
.Create(new Parameters(result.Properties));
19+
20+
return new BrowserResult
21+
{
22+
Response = url,
23+
ResultType = BrowserResultType.Success,
24+
};
25+
}
26+
catch (TaskCanceledException)
27+
{
28+
return new BrowserResult
29+
{
30+
ResultType = BrowserResultType.UserCancel
31+
};
32+
}
33+
}
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using Duende.IdentityModel.OidcClient;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace MauiApp1;
8+
public static class MauiProgram
9+
{
10+
public static MauiApp CreateMauiApp()
11+
{
12+
var builder = MauiApp.CreateBuilder();
13+
builder
14+
.UseMauiApp<App>()
15+
.ConfigureFonts(fonts =>
16+
{
17+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
18+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
19+
});
20+
21+
#if DEBUG
22+
builder.Logging.AddDebug();
23+
builder.Services.AddLogging(configure => configure.AddDebug());
24+
#endif
25+
26+
// setup OidcClient
27+
builder.Services.AddSingleton(new OidcClient(new()
28+
{
29+
Authority = "https://demo.duendesoftware.com",
30+
31+
ClientId = "interactive.public",
32+
Scope = "openid profile api",
33+
RedirectUri = "myapp://callback",
34+
35+
Browser = new MauiAuthenticationBrowser()
36+
}));
37+
38+
// add main page
39+
builder.Services.AddSingleton<MainPage>();
40+
41+
return builder.Build();
42+
}
43+
}

0 commit comments

Comments
 (0)