private async Task <HttpResponseMessage> GetTokens(string accessCodeOrRefreshToken, string requestType, string grantType) { using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(TokenUri))) { string requestContent = string.Format(TokenContentFormatAccess, clientId, OneDriveRedirectUrl, requestType, accessCodeOrRefreshToken, grantType); requestMessage.Content = new HttpStringContent(requestContent); requestMessage.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded"); using (HttpResponseMessage responseMessage = await httpClient.SendRequestAsync(requestMessage)) { string responseContentString = await responseMessage.Content.ReadAsStringAsync(); if (responseMessage.IsSuccessStatusCode) { JsonObject json; if (JsonObject.TryParse(responseContentString, out json)) { AccessToken = json.GetNamedString("access_token"); RefreshToken = json.GetNamedString("refresh_token"); } httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", AccessToken); TokensChangedEvent?.Invoke(this, new EventArgs()); LoggedIn = true; } return(responseMessage); } } }
/// <summary> /// Disposes of any user specific data obtained during login process. /// </summary> /// <returns>The response message given by the server for the request</returns> public async Task <HttpResponseMessage> LogoutAsync() { clientId = string.Empty; clientSecret = string.Empty; AccessToken = string.Empty; RefreshToken = string.Empty; if (null != refreshTimer) { refreshTimer.Dispose(); } if (null != httpClient) { httpClient.DefaultRequestHeaders.Clear(); } LoggedIn = false; TokensChangedEvent?.Invoke(this, new EventArgs()); string logoutUri = string.Format(OneDriveLogoutUrlFormat, clientId, OneDriveRedirectUrl); using (HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(logoutUri))) { using (HttpResponseMessage response = await httpClient.SendRequestAsync(requestMessage)) { return(response); } } }