|
12 | 12 | using Windows.Security.Authentication.Web.Core; |
13 | 13 | using Windows.Security.Credentials; |
14 | 14 | using Windows.Storage; |
| 15 | +using Windows.System; |
15 | 16 | using Windows.UI.ApplicationSettings; |
16 | 17 |
|
17 | 18 | namespace CommunityToolkit.Authentication |
@@ -197,7 +198,21 @@ public override async Task<string> GetTokenAsync(bool silentOnly = false) |
197 | 198 | } |
198 | 199 |
|
199 | 200 | // Attempt to authenticate interactively. |
200 | | - authResult = await AuthenticateInteractiveAsync(_scopes); |
| 201 | + var tcs = new TaskCompletionSource<WebTokenRequestResult>(); |
| 202 | + var taskQueued = DispatcherQueue.GetForCurrentThread().TryEnqueue(async () => |
| 203 | + { |
| 204 | + var result = await AuthenticateInteractiveAsync(_scopes); |
| 205 | + tcs.SetResult(result); |
| 206 | + }); |
| 207 | + |
| 208 | + if (taskQueued) |
| 209 | + { |
| 210 | + authResult = await tcs.Task; |
| 211 | + } |
| 212 | + else |
| 213 | + { |
| 214 | + tcs.SetCanceled(); |
| 215 | + } |
201 | 216 | } |
202 | 217 |
|
203 | 218 | if (authResult?.ResponseStatus == WebTokenRequestStatus.Success) |
@@ -241,74 +256,85 @@ public async Task ShowAccountManagementPaneAsync() |
241 | 256 | throw new InvalidOperationException("A logged in account is required to display the account management pane."); |
242 | 257 | } |
243 | 258 |
|
244 | | - // Build the AccountSettingsPane and configure it with available account commands. |
245 | | - void OnAccountCommandsRequested(AccountsSettingsPane sender, AccountsSettingsPaneCommandsRequestedEventArgs e) |
| 259 | + var tcs = new TaskCompletionSource<bool>(); |
| 260 | + _ = DispatcherQueue.GetForCurrentThread().TryEnqueue(async () => |
246 | 261 | { |
247 | | - AccountsSettingsPaneEventDeferral deferral = e.GetDeferral(); |
248 | | - |
249 | | - // Apply the configured header. |
250 | | - var headerText = _accountsSettingsPaneConfig?.ManageAccountHeaderText; |
251 | | - if (!string.IsNullOrWhiteSpace(headerText)) |
252 | | - { |
253 | | - e.HeaderText = headerText; |
254 | | - } |
255 | | - |
256 | | - // Generate any account commands. |
257 | | - if (_accountsSettingsPaneConfig?.AccountCommandParameter != null) |
| 262 | + AccountsSettingsPane pane = null; |
| 263 | + try |
258 | 264 | { |
259 | | - var commandParameter = _accountsSettingsPaneConfig.Value.AccountCommandParameter; |
260 | | - var webAccountCommand = new WebAccountCommand( |
261 | | - _webAccount, |
262 | | - async (command, args) => |
263 | | - { |
264 | | - // When the logout command is triggered, we also need to modify the state of the Provider. |
265 | | - if (args.Action == WebAccountAction.Remove) |
266 | | - { |
267 | | - await SignOutAsync(); |
268 | | - } |
| 265 | + // GetForCurrentView may throw an exception if the current view isn't ready yet. |
| 266 | + pane = AccountsSettingsPane.GetForCurrentView(); |
| 267 | + pane.AccountCommandsRequested += OnAccountCommandsRequested; |
269 | 268 |
|
270 | | - commandParameter.Invoked?.Invoke(command, args); |
271 | | - }, |
272 | | - commandParameter.Actions); |
| 269 | + // Show the AccountSettingsPane and wait for the result. |
| 270 | + await AccountsSettingsPane.ShowManageAccountsAsync(); |
273 | 271 |
|
274 | | - e.WebAccountCommands.Add(webAccountCommand); |
| 272 | + tcs.SetResult(true); |
275 | 273 | } |
276 | | - |
277 | | - // Apply any configured setting commands. |
278 | | - var commands = _accountsSettingsPaneConfig?.Commands; |
279 | | - if (commands != null) |
| 274 | + catch (Exception e) |
| 275 | + { |
| 276 | + tcs.SetException(e); |
| 277 | + } |
| 278 | + finally |
280 | 279 | { |
281 | | - foreach (var command in commands) |
| 280 | + if (pane != null) |
282 | 281 | { |
283 | | - e.Commands.Add(command); |
| 282 | + pane.AccountCommandsRequested -= OnAccountCommandsRequested; |
284 | 283 | } |
285 | 284 | } |
| 285 | + }); |
286 | 286 |
|
287 | | - deferral.Complete(); |
288 | | - } |
| 287 | + await tcs.Task; |
| 288 | + } |
289 | 289 |
|
290 | | - AccountsSettingsPane pane = null; |
291 | | - try |
292 | | - { |
293 | | - // GetForCurrentView may throw an exception if the current view isn't ready yet. |
294 | | - pane = AccountsSettingsPane.GetForCurrentView(); |
295 | | - pane.AccountCommandsRequested += OnAccountCommandsRequested; |
| 290 | + /// <summary> |
| 291 | + /// Build the AccountSettingsPane and configure it with available account commands. |
| 292 | + /// </summary> |
| 293 | + /// <param name="sender">The pane that fired the event.</param> |
| 294 | + /// <param name="e">Arguments for the AccountCommandsRequested event.</param> |
| 295 | + private void OnAccountCommandsRequested(AccountsSettingsPane sender, AccountsSettingsPaneCommandsRequestedEventArgs e) |
| 296 | + { |
| 297 | + AccountsSettingsPaneEventDeferral deferral = e.GetDeferral(); |
296 | 298 |
|
297 | | - // Show the AccountSettingsPane and wait for the result. |
298 | | - await AccountsSettingsPane.ShowManageAccountsAsync(); |
| 299 | + // Apply the configured header. |
| 300 | + var headerText = _accountsSettingsPaneConfig?.ManageAccountHeaderText; |
| 301 | + if (!string.IsNullOrWhiteSpace(headerText)) |
| 302 | + { |
| 303 | + e.HeaderText = headerText; |
299 | 304 | } |
300 | | - catch (Exception e) |
| 305 | + |
| 306 | + // Generate any account commands. |
| 307 | + if (_accountsSettingsPaneConfig?.AccountCommandParameter != null) |
301 | 308 | { |
302 | | - // TODO: Log exception |
303 | | - System.Diagnostics.Debug.WriteLine(e.Message); |
| 309 | + var commandParameter = _accountsSettingsPaneConfig.Value.AccountCommandParameter; |
| 310 | + var webAccountCommand = new WebAccountCommand( |
| 311 | + _webAccount, |
| 312 | + async (command, args) => |
| 313 | + { |
| 314 | + // When the logout command is triggered, we also need to modify the state of the Provider. |
| 315 | + if (args.Action == WebAccountAction.Remove) |
| 316 | + { |
| 317 | + await SignOutAsync(); |
| 318 | + } |
| 319 | + |
| 320 | + commandParameter.Invoked?.Invoke(command, args); |
| 321 | + }, |
| 322 | + commandParameter.Actions); |
| 323 | + |
| 324 | + e.WebAccountCommands.Add(webAccountCommand); |
304 | 325 | } |
305 | | - finally |
| 326 | + |
| 327 | + // Apply any configured setting commands. |
| 328 | + var commands = _accountsSettingsPaneConfig?.Commands; |
| 329 | + if (commands != null) |
306 | 330 | { |
307 | | - if (pane != null) |
| 331 | + foreach (var command in commands) |
308 | 332 | { |
309 | | - pane.AccountCommandsRequested -= OnAccountCommandsRequested; |
| 333 | + e.Commands.Add(command); |
310 | 334 | } |
311 | 335 | } |
| 336 | + |
| 337 | + deferral.Complete(); |
312 | 338 | } |
313 | 339 |
|
314 | 340 | private async Task SetAccountAsync(WebAccount account) |
|
0 commit comments