public async Task SetAccountAsync(MSACredentials account) { await this.LoadAsync(); if (this.profileData == null) { this.profileData = new ProfileDataContainerWrapper(); } this.profileData.Account = account; this.LastDateChecked = DateTime.UtcNow; this.profileData.LastDateChecked = this.LastDateChecked; await this.SaveAsync(); }
private async Task <bool> DeleteAsync( string endpoint, bool useCredentials = true, string overrideUri = null, CancellationTokenSource cts = null) { string uri = string.IsNullOrWhiteSpace(overrideUri) ? $"{BaseApiUri}/{endpoint}" : overrideUri; JsonDeleteNetworkRequest deleteRequest = useCredentials ? new JsonDeleteNetworkRequest( this.httpClient, uri, this.GetRequestHeaders()) : new JsonDeleteNetworkRequest(this.httpClient, uri); bool retryCall = false; try { await deleteRequest.ExecuteAsync <string>(cts); return(true); } catch (HttpRequestException hre) when(hre.Message.Contains("401")) { MSACredentials tokenRefreshed = await this.ExchangeRefreshTokenAsync(); if (tokenRefreshed != null) { retryCall = true; } } if (!retryCall) { return(false); } deleteRequest = useCredentials ? new JsonDeleteNetworkRequest(this.httpClient, uri, this.GetRequestHeaders()) : new JsonDeleteNetworkRequest(this.httpClient, uri); await deleteRequest.ExecuteAsync <string>(cts); return(true); }
public async Task ClearAsync() { await this.LoadAsync(); if (this.profileData == null) { this.profileData = new ProfileDataContainerWrapper(); } this.profileData.Account = null; this.profileData.Profile = null; this.profileData.ProfileImage = string.Empty; this.LastDateChecked = DateTime.MinValue; this.profileData.LastDateChecked = this.LastDateChecked; await this.SaveAsync(); }
private async Task AuthenticateAsync() { var scopes = new List <MSAScope> { MSAScope.Basic, MSAScope.Emails, MSAScope.OfflineAccess, MSAScope.SignIn }; string authUri = App.API.RetrieveAuthenticationUri(scopes); WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, new Uri(authUri), new Uri(ApiClient.RedirectUri)); if (result.ResponseStatus == WebAuthenticationStatus.Success) { if (!string.IsNullOrWhiteSpace(result.ResponseData)) { var responseUri = new Uri(result.ResponseData); if (responseUri.LocalPath.StartsWith("/oauth20_desktop.srf", StringComparison.OrdinalIgnoreCase)) { string error = responseUri.GetQueryValue("error"); if (string.IsNullOrWhiteSpace(error)) { string authCode = responseUri.GetQueryValue("code"); MSACredentials msaCredentials = await App.API.ExchangeAuthCodeAsync(authCode); IStorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(CredentialsFileName, CreationCollisionOption.OpenIfExists); await file.WriteTextAsync(JsonConvert.SerializeObject(msaCredentials)); } else { await App.MessageDialogManager.ShowAsync(error); } } } } this.UpdateButtonStates(); }
private async Task AuthenticateAsync() { List <MSAScope> scopes = new List <MSAScope> { MSAScope.Basic, MSAScope.Emails, MSAScope.OfflineAccess, MSAScope.SignIn }; string authUri = App.API.RetrieveAuthenticationUri(scopes); var result = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, new Uri(authUri), new Uri(ApiClient.RedirectUri)); if (result.ResponseStatus == WebAuthenticationStatus.Success) { if (!string.IsNullOrWhiteSpace(result.ResponseData)) { Uri responseUri = new Uri(result.ResponseData); if (responseUri.LocalPath.StartsWith("/oauth20_desktop.srf", StringComparison.OrdinalIgnoreCase)) { string error = responseUri.ExtractQueryValue("error"); if (string.IsNullOrWhiteSpace(error)) { string authCode = responseUri.ExtractQueryValue("code"); MSACredentials msaCredentials = await App.API.ExchangeAuthCodeAsync(authCode); } else { await MessageDialogManager.Current.ShowAsync(error); } } } } this.UpdateButtonStates(); }