示例#1
0
        public async Task <HttpWebApiResponse <string> > ValidateUser(string userName, string password)
        {
            try
            {
                var clientId     = userName;
                var clientSecret = password;
                var parameters   = new List <KeyValuePair <string, string> >();
                parameters.Add(new KeyValuePair <string, string>("grant_type", "client_credentials"));
                parameters.Add(new KeyValuePair <string, string>("role", "Beneficiary"));
                var client = _httpClientManager.GetOrAdd(_baseUrl);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(clientId + ":" + clientSecret)));
                var response = await client.ExtensionPostAsync <string>("token", parameters);

                return(response);
            }
            catch (Exception e)
            {
                var result = new HttpWebApiResponse <string> {
                    ErrorMessage = e.Message
                };
                return(result);
            }
        }
        public static async Task <HttpWebApiResponse> ExtensionPostAsJsonAsync(this HttpClient client, string requestUri, object value)
        {
            try
            {
                var jsonBody    = JsonConvert.SerializeObject(value);
                var buffer      = Encoding.UTF8.GetBytes(jsonBody);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var message = new HttpRequestMessage(HttpMethod.Post, requestUri);
                message.Content = byteContent;

                var httpResponse = await client.SendAsync(message).ConfigureAwait(false);

                if (!httpResponse.IsSuccessStatusCode)
                {
                    var response = new HttpWebApiResponse
                    {
                        ResponseStatus = ResponseStatuses.Failed,
                        ErrorMessage   = httpResponse.StatusCode + " " + httpResponse.ReasonPhrase
                    };
                    return(response);
                }
                // var responseResult = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
                return(new HttpWebApiResponse()
                {
                    ResponseStatus = ResponseStatuses.Success
                });
            }
            catch (Exception ex)
            {
                return(new HttpWebApiResponse
                {
                    ResponseStatus = ResponseStatuses.Failed,
                    ErrorMessage = ex.ToString()
                });
            }
        }