如何将基本身份验证与System.Net.Http.HttpClient一起使用?

编程入门 行业动态 更新时间:2024-10-28 11:20:49
本文介绍了如何将基本身份验证与System.Net.Http.HttpClient一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在c#核心中实现一个REST客户端,该客户端首先需要进行基本身份验证,然后在后续请求中利用Bearer令牌.

I'm trying to implement a rest client in c# core that needs to first do Basic Authentication, then leverage a Bearer token in subsequent requests.

当我尝试结合使用带有FormUrlEncodedContent对象的client.PostAsync进行基本身份验证时,出现了异常:

When I try to do Basic Authentication in combination with client.PostAsync with a FormUrlEncodedContent object, I'm getting an exception:

System.InvalidOperationException occurred in System.Net.Http.dll: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.'

//setup reusable http client HttpClient client = new HttpClient(); Uri baseUri = new Uri(url); client.BaseAddress = baseUri; client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.ConnectionClose = true; //Post body content var values = new List<KeyValuePair<string,string>>(); values.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); var content = new FormUrlEncodedContent(values); //Basic Authentication var authenticationString = $"{clientId}:{clientSecret}"; var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString)); content.Headers.Add("Authorization", $"Basic {base64EncodedAuthenticationString}"); //make the request var task = client.PostAsync("/oauth2/token",content); var response = task.Result; response.EnsureSuccessStatusCode(); string responseBody = response.Content.ReadAsStringAsync().Result; Console.WriteLine(responseBody);

Exception has occurred: CLR/System.InvalidOperationException An unhandled exception of type 'System.InvalidOperationException' occurred in System.Net.Http.dll: 'Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.' at System.Net.Http.Headers.HttpHeaders.GetHeaderDescriptor(String name) at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)

推荐答案

您似乎无法使用PostAsync,并且可以使用Headers进行身份验证.我必须使用HttpRequestMessage和SendAsync.

It looks like you can't use PostAsync and have access to mess with the Headers for authentication. I had to use an HttpRequestMessage and SendAsync.

//setup reusable http client HttpClient client = new HttpClient(); Uri baseUri = new Uri(url); client.BaseAddress = baseUri; client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.ConnectionClose = true; //Post body content var values = new List<KeyValuePair<string, string>>(); values.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); var content = new FormUrlEncodedContent(values); var authenticationString = $"{clientId}:{clientSecret}"; var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString)); var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/oauth2/token"); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString); requestMessage.Content = content; //make the request var task = client.SendAsync(requestMessage); var response = task.Result; response.EnsureSuccessStatusCode(); string responseBody = response.Content.ReadAsStringAsync().Result; Console.WriteLine(responseBody);

更多推荐

如何将基本身份验证与System.Net.Http.HttpClient一起使用?

本文发布于:2023-11-14 13:16:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   身份验证   System   HttpClient   Http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!