-
Notifications
You must be signed in to change notification settings - Fork 22
/
IotFriendlyWebDialog.xaml.cs
106 lines (92 loc) · 3.61 KB
/
IotFriendlyWebDialog.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Content Dialog item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace Microsoft.OneDrive.Sdk.Authentication
{
public sealed partial class IotFriendlyWebDialog : ContentDialog, INotifyPropertyChanged
{
private IDictionary<string, string> authenticationResponseValues = null;
private Uri callbackUri;
private TaskCompletionSource<bool> dialogTaskComplete = new TaskCompletionSource<bool>();
private Uri webViewSource;
public Uri WebViewSource
{
get { return webViewSource; }
internal set
{
if (webViewSource != value)
{
webViewSource = value;
RaisePropertyChanged();
}
}
}
public IotFriendlyWebDialog()
{
this.InitializeComponent();
}
public async Task<IDictionary<string, string>> GetAuthenticationResponseValue(Uri requestUri, Uri callbackUri)
{
bool isSignOutRequest =
requestUri.AbsoluteUri.StartsWith(OAuthConstants.MicrosoftAccountSignOutUrl,
StringComparison.OrdinalIgnoreCase);
this.callbackUri = callbackUri;
this.DialogWebView.Navigate(requestUri);
if (!isSignOutRequest)
{
await Task.WhenAll(this.ShowAsync().AsTask(), this.dialogTaskComplete.Task);
}
else
{
await dialogTaskComplete.Task;
}
return this.authenticationResponseValues;
}
private void ContentDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
this.dialogTaskComplete.TrySetResult(true);
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.authenticationResponseValues = null;
this.Hide();
this.dialogTaskComplete.TrySetResult(true);
}
private void DialogWebView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
if (this.NavigatedToCallbackUrl(args.Uri))
{
args.Cancel = true;
this.authenticationResponseValues = UrlHelper.GetQueryOptions(args.Uri);
this.Hide();
this.dialogTaskComplete.TrySetResult(true);
}
}
private void DialogWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
if (this.NavigatedToCallbackUrl(args.Uri))
{
this.authenticationResponseValues = UrlHelper.GetQueryOptions(args.Uri);
this.dialogTaskComplete.TrySetResult(true);
this.Hide();
}
}
private bool NavigatedToCallbackUrl(Uri uri)
{
return uri.Authority.Equals(
this.callbackUri.Authority, StringComparison.OrdinalIgnoreCase)
&& uri.AbsolutePath.Equals(this.callbackUri.AbsolutePath);
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName]string property = "")
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}