-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(msal): Added a sample for MSAL+Graph
- Loading branch information
1 parent
7418302
commit 4ce9339
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/SamplesApp/SamplesApp.Wasm/wwwroot/authentication/login-callback.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title></title> | ||
</head> | ||
<body> | ||
Please wait few seconds... | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
110 changes: 110 additions & 0 deletions
110
src/SamplesApp/UITests.Shared/Msal/MsalLoginAndGraph.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters