Skip to content

Commit

Permalink
Minor update to a sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Feb 2, 2021
1 parent 1f79dd9 commit 8bf722b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
39 changes: 39 additions & 0 deletions tests/devapps/WAM/UWPWam/DispatcherExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Core;

namespace UWP_standalone
{
public static class DispatcherExtensions
{
private static async Task<T> RunOnUiThreadAsync<T>(CoreDispatcher dispatcher,
Func<Task<T>> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal)
{
var taskCompletionSource = new TaskCompletionSource<T>();
await dispatcher.RunAsync(priority, async () =>
{
try
{
taskCompletionSource.SetResult(await func().ConfigureAwait(false));
}
catch (Exception ex)
{
taskCompletionSource.SetException(ex);
}
});
return await taskCompletionSource.Task.ConfigureAwait(false);
}

public static async Task<T> RunOnUiThreadAsync<T>(Func<Task<T>> func)
{
return await RunOnUiThreadAsync<T>(
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher,
func, CoreDispatcherPriority.Normal).ConfigureAwait(true);
}


}
}
63 changes: 50 additions & 13 deletions tests/devapps/WAM/UWPWam/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Windows.UI.Core;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

Expand Down Expand Up @@ -56,12 +57,26 @@ public MainPage()

private IPublicClientApplication CreatePublicClient()
{
return PublicClientApplicationBuilder.Create(s_clientID)
.WithAuthority(s_authority)
.WithExperimentalFeatures(true)
.WithBroker(chkUseBroker.IsChecked.Value)
.WithLogging((x, y, z) => Debug.WriteLine($"{x} {y}"), LogLevel.Verbose, true)
.Build();
var pca = PublicClientApplicationBuilder.Create(s_clientID)
.WithAuthority(s_authority)
.WithExperimentalFeatures(true)
.WithBroker(true)
.WithLogging((x, y, z) => Debug.WriteLine($"{x} {y}"), LogLevel.Verbose, true)
.Build();

return pca;
//return DispatcherExtensions.RunOnUiThreadAsync(
//() =>
//{
// var pca = PublicClientApplicationBuilder.Create(s_clientID)
// .WithAuthority(s_authority)
// .WithExperimentalFeatures(true)
// .WithBroker(true)
// .WithLogging((x, y, z) => Debug.WriteLine($"{x} {y}"), LogLevel.Verbose, true)
// .Build();

// return Task.FromResult(pca);
//}).GetAwaiter().GetResult();
}

private async void AcquireTokenIWA_ClickAsync(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -171,18 +186,37 @@ private async void ATS_ClickAsync(object sender, RoutedEventArgs e)

private async void ATI_ClickAsync(object sender, RoutedEventArgs e)
{
await Task.Delay(1000).ConfigureAwait(false);
var sc = SynchronizationContext.Current; // should be null

var pca = CreatePublicClient();
var upnPrefix = tbxUpn.Text;
//var upnPrefix = tbxUpn.Text;

IEnumerable<IAccount> accounts = await pca.GetAccountsAsync().ConfigureAwait(true); // stay on UI thread
var acc = accounts.SingleOrDefault(a => a.Username.StartsWith(upnPrefix));
//IEnumerable<IAccount> accounts = await pca.GetAccountsAsync().ConfigureAwait(true); // stay on UI thread
//var acc = accounts.SingleOrDefault(a => a.Username.StartsWith(upnPrefix));

try
{
var result = await pca.AcquireTokenInteractive(s_scopes)
.WithAccount(acc)
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);
AuthenticationResult result = null;
//await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
// Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
// {
//result = await pca.AcquireTokenInteractive(s_scopes)
// //.WithAccount(acc)
// .ExecuteAsync(CancellationToken.None)
// .ConfigureAwait(false);
//});

result = await
DispatcherExtensions.RunOnUiThreadAsync(
async () =>
{
return await pca.AcquireTokenInteractive(s_scopes)
//.WithAccount(acc)
.ExecuteAsync(CancellationToken.None)
.ConfigureAwait(false);
}).ConfigureAwait(false);

await DisplayResultAsync(result).ConfigureAwait(false);

Expand All @@ -196,6 +230,9 @@ private async void ATI_ClickAsync(object sender, RoutedEventArgs e)

}




private async Task DisplayErrorAsync(Exception ex)
{
await DisplayMessageAsync(ex.ToString()).ConfigureAwait(false);
Expand Down
3 changes: 2 additions & 1 deletion tests/devapps/WAM/UWPWam/UWP standalone WAM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="DispatcherExt.cs" />
<Compile Include="DpApiProxy.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
Expand Down Expand Up @@ -170,4 +171,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

0 comments on commit 8bf722b

Please sign in to comment.