public void AddRequestGuid(RequestGuid requestGuid)
        {
            ExpirationWrapper <RequestGuid> wrapper = new ExpirationWrapper <RequestGuid>(requestGuid, REQUEST_TIMEOUT_MILLISECONDS);

            wrapper.Expired += RequestExpiredHandler;
            wrapper.RestartTimer();
            if (!_authRequests.TryAdd(requestGuid.ToString(), wrapper))
            {
                throw new InvalidOperationException("Request already exists.");
            }
        }
        public bool VerifyAuthRequest(string requestGuidString)
        {
            RequestGuid guid = _authMgr.GetRequestGuid(requestGuidString);

            if (guid != null && guid.ToString() == requestGuidString)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        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());
        }