public async Task <bool> RequestLoginAsync() { foreach (var user in _pca.Users) { _pca.Remove(user); } //ms-app://s-1-15-2-3388634293-103715313-3266902762-3262461205-4164087508-4155876753-1602774140/ var userByPolicy = GetUserByPolicy(_pca.Users, _policySignUpSignIn); await _pca.AcquireTokenAsync(_scopes, userByPolicy, _uiParent); //TODO if we reach here, is there a chance login was not successful? return(true); }
public async Task <AuthenticationResult> AcquireTokenAsync() { if (UiParent == null) { Activity currentActivity = null; try { currentActivity = CurrentActivityHelper.GetCurrentActivity(); UiParent = new UIParent(currentActivity); } catch (Exception ex) { logger.Log(ex, "Error getting current activity"); } } AuthenticationResult res = null; try { res = await publicClientApplication.AcquireTokenAsync(AuthConfig.Scopes, UiParent); } catch (Exception ex1) { logger.Log(ex1, "Error aquiring token"); } return(res); }
private async Task <AuthenticationResult> GetNewAccessTokenAsync(IAccount account, string[] scopes) { IPublicClientApplication publicClientApplication = (ClientApplication as IPublicClientApplication); AuthenticationResult authenticationResult = null; int retryCount = 0; string extraQueryParameter = null; do { try { authenticationResult = await publicClientApplication.AcquireTokenAsync(scopes, account, UIBehavior, extraQueryParameter, null, ClientApplication.Authority, UIParent); break; } catch (MsalServiceException serviceException) { if (serviceException.ErrorCode == ErrorConstants.Codes.TemporarilyUnavailable) { TimeSpan delay = this.GetRetryAfter(serviceException); retryCount++; // pause execution await Task.Delay(delay); } else if (serviceException.Claims != null) { extraQueryParameter = $"claims={serviceException.Claims}"; retryCount++; } else { throw new AuthenticationException( new Error { Code = ErrorConstants.Codes.GeneralException, Message = ErrorConstants.Message.UnexpectedMsalException }, serviceException); } } catch (Exception exception) { throw new AuthenticationException( new Error { Code = ErrorConstants.Codes.GeneralException, Message = ErrorConstants.Message.UnexpectedException }, exception); } } while (retryCount < MaxRetry); return(authenticationResult); }
private async Task <AuthResult> AcquireTokenAsync(IPublicClientApplication app, IEnumerable <string> scopes, string usrId) { var acct = !string.IsNullOrEmpty(usrId) ? await app.GetAccountAsync(usrId) : null; var userStr = acct != null ? acct.Username : "******"; Debug.Log($"Found User {userStr}"); AuthResult res = new AuthResult(); try { Debug.Log($"Calling AcquireTokenSilentAsync"); res.res = await app.AcquireTokenSilentAsync(scopes, acct).ConfigureAwait(false); Debug.Log($"app.AcquireTokenSilentAsync called {res.res}"); } catch (MsalUiRequiredException) { Debug.Log($"Needs UI for Login"); try { res.res = await app.AcquireTokenAsync(scopes).ConfigureAwait(false); Debug.Log($"app.AcquireTokenAsync called {res.res}"); } catch (MsalException msalex) { res.err = $"Error Acquiring Token:{Environment.NewLine}{msalex.Message}"; Debug.Log($"{res.err}"); return(res); } catch (Exception ex) { res.err = $"Error Acquiring Token Silently:{Environment.NewLine}{ex.Message}"; Debug.Log($"{res.err}"); return(res); } } catch (Exception ex) { res.err = $"Error Acquiring Token Silently:{Environment.NewLine}{ex.Message}"; Debug.Log($"{res.err}"); return(res); } #if !UNITY_EDITOR && UNITY_WSA Debug.Log($"Access Token - {res.res.AccessToken}"); ApplicationData.Current.LocalSettings.Values["UserId"] = res.res.Account.HomeAccountId.Identifier; #endif return(res); }
public async Task <AuthResult> AcquireTokenAsync(IPublicClientApplication app, IEnumerable <string> scopes, string usrId, Func <AuthenticationResult, Task> callback) { var usr = !string.IsNullOrEmpty(usrId) ? await app.GetAccountAsync(usrId) : null; var userStr = usr != null ? usr.Username : "******"; //Debug.Log($"Found User {userStr}"); AuthResult res = new AuthResult(); try { //Debug.Log($"Calling AcquireTokenSilentAsync"); res.res = await app.AcquireTokenSilentAsync(scopes, usr).ConfigureAwait(false); //Debug.Log($"app.AcquireTokenSilentAsync called {res.res}"); } catch (MsalUiRequiredException) { //Debug.Log($"Needs UI for Login"); try { res.res = await app.AcquireTokenAsync(scopes).ConfigureAwait(false); if (callback != null) { await callback(res.res); } //Debug.Log($"app.AcquireTokenAsync called {res.res}"); } catch (MsalException msalex) { res.err = $"Error Acquiring Token:{Environment.NewLine}{msalex}"; //Debug.Log($"{res.err}"); return(res); } } catch (Exception ex) { res.err = $"Error Acquiring Token Silently:{Environment.NewLine}{ex}"; //Debug.Log($"{res.err}"); return(res); } #if !UNITY_EDITOR && UNITY_WSA Debug.Log($"Access Token - {res.res.AccessToken}"); ApplicationData.Current.LocalSettings.Values["UserId"] = res.res.User.Identifier; #endif return(res); }
private async void AccessTokenButton_ClickAsync(object sender, RoutedEventArgs e) { AuthenticationResult result = null; try { result = await _pca.AcquireTokenAsync(Scopes).ConfigureAwait(false); } catch (Exception ex) { await DisplayErrorAsync(ex).ConfigureAwait(false); return; } await DisplayResultAsync(result).ConfigureAwait(false); }
private async void AccessTokenButton_ClickAsync(object sender, RoutedEventArgs e) { AuthenticationResult result = null; try { var users = await _pca.GetAccountsAsync().ConfigureAwait(false); var user = users.FirstOrDefault(); result = await _pca.AcquireTokenAsync(Scopes, user, UIBehavior.ForceLogin, "").ConfigureAwait(false); } catch (Exception ex) { await DisplayErrorAsync(ex).ConfigureAwait(false); return; } await DisplayResultAsync(result).ConfigureAwait(false); }
/// <summary> /// Attempt to retrieve the Access Token by either retrieving /// previously stored credentials or by prompting user to Login /// </summary> private async Task <AuthenticationResult> AcquireTokenAsync( IPublicClientApplication app, IEnumerable <string> scopes, string userId) { IUser user = !string.IsNullOrEmpty(userId) ? app.GetUser(userId) : null; string userName = user != null ? user.Name : "null"; // Once the User name is found, display it as a welcome message MeetingsUI.Instance.WelcomeUser(userName); // Attempt to Log In the user with a pre-stored token. Only happens // in case the user Logged In with this app on this device previously try { _authResult = await app.AcquireTokenSilentAsync(scopes, user); } catch (MsalUiRequiredException) { // Pre-stored token not found, prompt the user to log-in try { _authResult = await app.AcquireTokenAsync(scopes); } catch (MsalException msalex) { Debug.Log($"Error Acquiring Token: {msalex.Message}"); return(_authResult); } } MeetingsUI.Instance.WelcomeUser(_authResult.User.Name); #if !UNITY_EDITOR && UNITY_WSA ApplicationData.Current.LocalSettings.Values["UserId"] = _authResult.User.Identifier; #endif return(_authResult); }
public async Task <AuthenticationResult> AcquireTokenAsync() => await publicClientApplication.AcquireTokenAsync(AuthConfig.Scopes, new UIParent());