feat(msal): Added a sample for MSAL+Graph
This commit is contained in:
Родитель
7418302c49
Коммит
4ce9339261
|
@ -252,3 +252,4 @@ msbuild.binlog
|
|||
/src/SamplesApp/SamplesApp.Wasm.UITests/out
|
||||
/src/Uno.UI.TestComparer/Properties/launchSettings.json
|
||||
|
||||
src/AddIns/Uno.UI.MSAL/WasmScripts/*.d.ts
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
Please wait few seconds...
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,34 @@
|
|||
<Page
|
||||
x:Class="UITests.Msal.MsalLoginAndGraph"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d"
|
||||
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
|
||||
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" RowSpacing="12" ColumnSpacing="5" Margin="10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Button Click="SignIn" Grid.Row="0" Grid.Column="1">Sign-In Interactively</Button>
|
||||
<TextBlock Grid.Row="1" Grid.Column="0">Token:</TextBlock>
|
||||
<TextBox x:Name="tokenBox" Grid.Row="1" Grid.Column="1" />
|
||||
<Button Click="LoadFromGraph" Grid.Row="2" Grid.Column="0">Load from Graph</Button>
|
||||
<StackPanel Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left">
|
||||
<Image x:Name="thumbnail" Width="150" Height="150" />
|
||||
<TextBlock x:Name="name" FontSize="18" />
|
||||
</StackPanel>
|
||||
<TextBlock Grid.Row="3" Grid.Column="1" FontSize="15" Foreground="Red">
|
||||
IMPORTANT: for this to work on Wasm, IT MUST BE SERVED
|
||||
ON THE URL http://localhost:55838/.
|
||||
</TextBlock>
|
||||
</Grid>
|
||||
</Page>
|
|
@ -0,0 +1,110 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Graph;
|
||||
using Microsoft.Identity.Client;
|
||||
using Uno.Extensions;
|
||||
using Uno.UI.MSAL;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
using Uno.UI.Samples.Controls;
|
||||
using Prompt = Microsoft.Identity.Client.Prompt;
|
||||
|
||||
namespace UITests.Msal
|
||||
{
|
||||
[Sample("MSAL", IgnoreInSnapshotTests = true)]
|
||||
public sealed partial class MsalLoginAndGraph : Page, IAuthenticationProvider
|
||||
{
|
||||
private const string CLIENT_ID = "a74f513b-2d8c-45c0-a15a-15e63f7a7862";
|
||||
private const string TENANT_ID = "6d53ef61-b6d1-4150-ae0b-43b90e75e0cd";
|
||||
|
||||
#if __WASM__
|
||||
private const string REDIRECT_URI = "http://localhost:55838/authentication/login-callback.htm";
|
||||
#elif __IOS__ || __MACOS__
|
||||
private const string REDIRECT_URI = "msal" + CLIENT_ID + "://auth";
|
||||
#elif __ANDROID__
|
||||
private const string REDIRECT_URI = "msauth://SamplesApp.Droid/BUWXtvbCbxw6rdZidSYhNH6gLvA%3D";
|
||||
#else
|
||||
private const string REDIRECT_URI = "https://login.microsoftonline.com/common/oauth2/nativeclient";
|
||||
#endif
|
||||
|
||||
private readonly string[] SCOPES = new[] { "https://graph.microsoft.com/User.Read", "https://graph.microsoft.com/email", "https://graph.microsoft.com/profile" };
|
||||
|
||||
private IPublicClientApplication _app;
|
||||
|
||||
public MsalLoginAndGraph()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
|
||||
_app = PublicClientApplicationBuilder
|
||||
.Create(CLIENT_ID)
|
||||
.WithTenantId(TENANT_ID)
|
||||
.WithRedirectUri(REDIRECT_URI)
|
||||
.WithUnoHelpers()
|
||||
#if __IOS__ || __MACOS__
|
||||
.WithIosKeychainSecurityGroup("com.companyname.SamplesApp")
|
||||
#endif
|
||||
.Build();
|
||||
}
|
||||
|
||||
private async void SignIn(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var result = await _app.AcquireTokenInteractive(SCOPES)
|
||||
.WithPrompt(Prompt.SelectAccount)
|
||||
.WithUnoHelpers()
|
||||
.ExecuteAsync();
|
||||
|
||||
tokenBox.Text = result.AccessToken;
|
||||
}
|
||||
|
||||
private async void LoadFromGraph(object sender, RoutedEventArgs e)
|
||||
{
|
||||
#if __WASM__
|
||||
var http = new HttpClient(new Uno.UI.Wasm.WasmHttpHandler());
|
||||
#else
|
||||
var http = new HttpClient();
|
||||
#endif
|
||||
var httpClient = http;
|
||||
var client = new GraphServiceClient(httpClient);
|
||||
client.AuthenticationProvider = this;
|
||||
|
||||
try
|
||||
{
|
||||
using (var stream = await client.Me.Photo.Content.Request().GetAsync())
|
||||
{
|
||||
var bitmap = new BitmapImage();
|
||||
#if HAS_UNO
|
||||
bitmap.SetSource(new MemoryStream(stream.ReadBytes()));
|
||||
#else
|
||||
await bitmap.SetSourceAsync(stream.AsRandomAccessStream());
|
||||
#endif
|
||||
thumbnail.Source = bitmap;
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.Error.WriteLine(exception);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var me = await client.Me.Request().GetAsync();
|
||||
|
||||
name.Text = me.DisplayName;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.Error.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
|
||||
Task IAuthenticationProvider.AuthenticateRequestAsync(HttpRequestMessage request)
|
||||
{
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokenBox.Text);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,6 +41,10 @@
|
|||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="$(MSBuildThisFileDirectory)Msal\MsalLoginAndGraph.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="$(MSBuildThisFileDirectory)Resources\StaticResource\StaticResource_Simple.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
|
@ -3251,6 +3255,9 @@
|
|||
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\TwoPaneViewTests\TwoPaneViewPage.xaml.cs">
|
||||
<DependentUpon>TwoPaneViewPage.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Msal\MsalLoginAndGraph.xaml.cs">
|
||||
<DependentUpon>MsalLoginAndGraph.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Toolkit\ElevatedViewTests.xaml.cs">
|
||||
<DependentUpon>ElevatedViewTests.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
@ -5804,4 +5811,4 @@
|
|||
<None Include="$(MSBuildThisFileDirectory)ItemExclusions.props" />
|
||||
</ItemGroup>
|
||||
<Import Project="ItemExclusions.props" />
|
||||
</Project>
|
||||
</Project>
|
Загрузка…
Ссылка в новой задаче