public async Task <String> GetAccessTokenAsync(IReadOnlyList <string> scopes) { if (Type == ConnectedDevicesAccountType.MSA) { return(await MSAOAuthHelpers.GetAccessTokenUsingRefreshTokenAsync(Token, scopes)); } else if (Type == ConnectedDevicesAccountType.AAD) { var authContext = new AuthenticationContext("https://login.microsoftonline.com/common"); UserIdentifier aadUserId = new UserIdentifier(Id, UserIdentifierType.UniqueId); AuthenticationResult result; try { result = await authContext.AcquireTokenSilentAsync(scopes[0], Secrets.AAD_CLIENT_ID); } catch (Exception ex) { Logger.Instance.LogMessage($"Token request failed: {ex.Message}"); // Token may have expired, try again non-silently result = await authContext.AcquireTokenAsync(scopes[0], Secrets.AAD_CLIENT_ID, new Uri(Secrets.AAD_REDIRECT_URI), new PlatformParameters(PromptBehavior.Auto, true)); } return(result.AccessToken); } else { throw new Exception("Invalid Account Type"); } }
private async Task <ConnectedDevicesAccessTokenResult> GetMsaTokenForUserAsync(IReadOnlyList <string> scopes) { try { string accessToken = await MSAOAuthHelpers.GetAccessTokenUsingRefreshTokenAsync(MsaToken, scopes); Logger.Instance.LogMessage($"MSA Token : {accessToken}"); return(new ConnectedDevicesAccessTokenResult(accessToken, ConnectedDevicesAccessTokenRequestStatus.Success)); } catch (Exception ex) { Logger.Instance.LogMessage($"MSA Token request failed: {ex.Message}"); return(new ConnectedDevicesAccessTokenResult(string.Empty, ConnectedDevicesAccessTokenRequestStatus.TransientError)); } }
public async Task <bool> SignInMsaAsync() { string refreshToken = await MSAOAuthHelpers.GetRefreshTokenAsync(); if (!string.IsNullOrEmpty(refreshToken)) { var account = new Account(m_platform, Guid.NewGuid().ToString(), ConnectedDevicesAccountType.MSA, refreshToken, AccountRegistrationState.InAppCacheOnly); m_accounts.Add(account); await account.InitializeAccountAsync(); AccountListChanged(); return(true); } return(false); }
public async Task <bool> SignInMsa() { string refreshToken = string.Empty; if (ApplicationData.Current.LocalSettings.Values.ContainsKey(MsaTokenKey)) { refreshToken = ApplicationData.Current.LocalSettings.Values[MsaTokenKey] as string; } if (string.IsNullOrEmpty(refreshToken)) { refreshToken = await MSAOAuthHelpers.GetRefreshTokenAsync(); } if (!string.IsNullOrEmpty(refreshToken)) { MsaToken = refreshToken; ApplicationData.Current.LocalSettings.Values[MsaTokenKey] = refreshToken; SignedInAccount = new ConnectedDevicesUserAccount(Guid.NewGuid().ToString(), ConnectedDevicesUserAccountType.MSA); return(true); } return(false); }