public async Task <string> GetAuthorizationUrlAsync() { RequestGuid guid = new RequestGuid(); string guidString = guid.ToString(); Dictionary <string, string> query = new Dictionary <string, string> { { "client_id", OAUTH_CLIENT_ID }, { "response_type", "code" }, { "state", guidString }, { "redirect_uri", REDIRECT_URI }, }; HttpResponseMessage response = await _client.GetAsync(QueryHelpers.AddQueryString(AUTH_URI, query)); /* URI is encoded twice, so need to decode twice to get the query string */ string firstDecode = WebUtility.UrlDecode(response.RequestMessage.RequestUri.ToString()); string resultUri = WebUtility.UrlDecode(firstDecode); int queryIndex = resultUri.IndexOf('?'); if (queryIndex < 0) { throw new InvalidQueryException("No query string returned by authorization request."); } Dictionary <string, StringValues> queryValues = QueryHelpers.ParseNullableQuery(resultUri.Substring(queryIndex)); if (queryValues == null) { throw new InvalidQueryException("Invalid query string returned by authorization request."); } string stateReturned = queryValues["state"]; if (stateReturned != guidString) { throw new InvalidOperationException("Incorrect state returned by authorization request."); } /* Request for authorization URL was successful */ _authMgr.AddRequestGuid(guid); return(response.RequestMessage.RequestUri.ToString()); }