private HttpRequestMessage CreateRequestMessage(OpenIddictRequest request, HttpMethod method, Uri uri)
    {
        // Note: a dictionary is deliberately not used here to allow multiple parameters with the
        // same name to be specified. While initially not allowed by the core OAuth2 specification,
        // this is required for derived drafts like the OAuth2 token exchange specification.
        var parameters = new List <KeyValuePair <string?, string?> >();

        foreach (var parameter in request.GetParameters())
        {
            // If the parameter is null or empty, send an empty value.
            if (OpenIddictParameter.IsNullOrEmpty(parameter.Value))
            {
                parameters.Add(new KeyValuePair <string?, string?>(parameter.Key, string.Empty));

                continue;
            }

            var values = (string?[]?)parameter.Value;
            if (values is not {
                Length: > 0
            })
        private HttpRequestMessage CreateRequestMessage(OpenIddictRequest request, HttpMethod method, Uri uri)
        {
            // Note: a dictionary is deliberately not used here to allow multiple parameters with the
            // same name to be specified. While initially not allowed by the core OAuth2 specification,
            // this is required for derived drafts like the OAuth2 token exchange specification.
            var parameters = new List <KeyValuePair <string, string> >();

            foreach (var parameter in request.GetParameters())
            {
                // If the parameter is null or empty, send an empty value.
                if (OpenIddictParameter.IsNullOrEmpty(parameter.Value))
                {
                    parameters.Add(new KeyValuePair <string, string>(parameter.Key, string.Empty));

                    continue;
                }

                var values = (string[])parameter.Value;
                if (values == null || values.Length == 0)
                {
                    continue;
                }

                foreach (var value in values)
                {
                    parameters.Add(new KeyValuePair <string, string>(parameter.Key, value));
                }
            }

            if (method == HttpMethod.Get && parameters.Count != 0)
            {
                var builder = new StringBuilder();

                foreach (var parameter in parameters)
                {
                    if (builder.Length != 0)
                    {
                        builder.Append('&');
                    }

                    builder.Append(UrlEncoder.Default.Encode(parameter.Key));
                    builder.Append('=');
                    builder.Append(UrlEncoder.Default.Encode(parameter.Value));
                }

                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri(HttpClient.BaseAddress, uri);
                }

                uri = new UriBuilder(uri)
                {
                    Query = builder.ToString()
                }.Uri;
            }

            var message = new HttpRequestMessage(method, uri);

            if (method != HttpMethod.Get)
            {
                message.Content = new FormUrlEncodedContent(parameters);
            }

            return(message);
        }