public async Task <bool> SignInAsync() { try { IEnumerable <IAccount> accounts = await _pca.GetAccountsAsync().ConfigureAwait(false); IAccount firstAccount = accounts.FirstOrDefault(); AuthenticationResult authResult = await _pca.AcquireTokenSilent(Scopes, firstAccount) .ExecuteAsync().ConfigureAwait(false); // Store the access token securely for later use. string authToken = authResult?.AccessToken; await SecureStorage.SetAsync("AccessToken", authToken).ConfigureAwait(false); AuthorizedDelegate?.Invoke(authToken); string savedToken = await SecureStorage.GetAsync("AccessToken").ConfigureAwait(false); Debug.WriteLine("STORED TOKEN: " + (savedToken != null ? savedToken?.ToString() : "Not Saved")); return(true); } catch (MsalUiRequiredException) { try { // This means we need to login again through the MSAL window. AuthenticationResult authResult = await _pca.AcquireTokenInteractive(Scopes) .WithParentActivityOrWindow(ParentWindow) .ExecuteAsync().ConfigureAwait(false); // Store the access token securely for later use. string authToken = authResult?.AccessToken; await SecureStorage.SetAsync("AccessToken", authToken).ConfigureAwait(false); AuthorizedDelegate?.Invoke(authToken); string savedToken = await SecureStorage.GetAsync("AccessToken").ConfigureAwait(false); Debug.WriteLine("MSAL TOKEN: " + (savedToken != null ? savedToken?.ToString() : "Not Saved")); return(true); } catch (Exception ex2) { Debug.WriteLine(ex2.ToString()); return(false); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); return(false); } }
public async Task AuthenticateAsync(string idProvider, string idProviderToken) { var token = new IdProviderToken { AccessToken = idProviderToken }; var url = new Uri(_baseUri, string.Format(".auth/login/{0}", idProvider)); var response = await SendRequestAsync <CliffWarsApiAuthToken>(url, HttpMethod.Post, requestData : token); _id = response.User.UserId; Preferences.Set("UserId", _id); if (!string.IsNullOrWhiteSpace(response?.AuthenticationToken)) { var authToken = response.AuthenticationToken; _headers["x-zumo-auth"] = authToken; AuthorizedDelegate?.Invoke(authToken); } }
public async Task AuthenticateAsync(string idProvider, string idProviderToken) { var token = new IdProviderToken { AccessToken = idProviderToken }; var url = new Uri(_baseUri, string.Format(".auth/login/{0}", idProvider)); var response = await SendRequestAsync <TripLogApiAuthToken>(url, HttpMethod.Post, requestData : token); if (!string.IsNullOrWhiteSpace(response?.AuthenticationToken)) { var authToken = response.AuthenticationToken; // Update this service with the new auth token _headers["x-zumo-auth"] = authToken; AuthorizedDelegate?.Invoke(authToken); } }