private async Task <AuthResult> AcquireTokenAsync(IPublicClientApplication app, IEnumerable <string> scopes, string usrId) { var usr = !string.IsNullOrEmpty(usrId) ? app.GetUser(usrId) : null; var userStr = usr != null ? usr.Name : "null"; 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); 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); }
/// <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); }