public async Task ResetPassword(Auth_VM AuthB2C) { try { var accounts = await pca.GetAccountsAsync(); var account = Helpers.GetAccountByPolicy(accounts, AuthB2C.AuthorityResetPassword); var ar = await pca.AcquireTokenInteractive(this.scopes) .WithAccount(account) .WithB2CAuthority(AuthB2C.AuthorityResetPassword) .WithParentActivityOrWindow(parentActivity) .ExecuteAsync(); } catch (Exception ex) { // Alert if any exception excludig user cancelling sign-in dialog if (((ex as MsalException)?.ErrorCode != "authentication_canceled")) { throw ex; } } }
public async Task <bool> Connect(Auth_VM AuthB2C, bool isSilent, string previousSignInName) { var stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); System.Diagnostics.Debug.WriteLine($"Auth.Connect.Start "); var accounts = await pca.GetAccountsAsync(); if (accounts != null && accounts.Count() > 1) { System.Windows.MessageBox.Show("Multiple cached accounts discovered"); foreach (var item in accounts) { System.Diagnostics.Debug.WriteLine($" - {item.ToString()}"); } } var firstAccount = accounts.FirstOrDefault(); try { if (isSilent) { authResult = await pca.AcquireTokenSilent(this.scopes, firstAccount).ExecuteAsync(); } else { System.Diagnostics.Debug.WriteLine($"Auth.Connect.SignOut @ {stopWatch.ElapsedMilliseconds / 1000}"); await SignOut(); System.Diagnostics.Debug.WriteLine($"Auth.Connect.AcquireTokenInteractive @ {stopWatch.ElapsedMilliseconds / 1000}"); authResult = await pca.AcquireTokenInteractive(this.scopes) .WithUseEmbeddedWebView(true) .WithLoginHint(previousSignInName) //.WithAccount(firstAccount) .WithParentActivityOrWindow(parentActivity) .WithPrompt(Prompt.SelectAccount) .ExecuteAsync(); System.Diagnostics.Debug.WriteLine($"Auth.Connect.AcquireTokenInteractive Success @ {stopWatch.ElapsedMilliseconds / 1000}"); } } catch (MsalUiRequiredException exMsal) { System.Diagnostics.Debug.WriteLine($"Auth.Connect.exMsal @ {stopWatch.ElapsedMilliseconds / 1000}"); throw exMsal; } catch (Microsoft.Identity.Client.MsalServiceException exMsal2) { System.Diagnostics.Debug.WriteLine($"Auth.Connect.exMsal2 @ {stopWatch.ElapsedMilliseconds / 1000}"); if (exMsal2.Message.Contains("AADB2C90118") == true) //The user has forgotten their password. { await ResetPassword(AuthB2C); } else if (exMsal2.Message.Contains("AADB2C90091") == true) //The user has cancelled entering self-asserted information.) { return(false); } else { throw exMsal2; } } catch (Microsoft.Identity.Client.MsalClientException exMsal3) { // just cancelled, ignore? System.Diagnostics.Debug.WriteLine($"Auth.Connect.exMsal3 @ {stopWatch.ElapsedMilliseconds / 1000}"); Debug.WriteLine(exMsal3.ToString()); return(false); } catch (Exception ex) { throw ex; } finally { System.Diagnostics.Debug.WriteLine($"Auth.Connect took {stopWatch.ElapsedMilliseconds / 1000} seconds"); } // if we get this far, we have validated succesfully, set up the creds and decode the user claims this.creds = new TokenCredentials(authResult.IdToken); this.userFromClaims = new UserFromClaims(authResult.IdToken); return(true); }