RestSharp - WCF REST 服务未遇到授权标头 [英] RestSharp - Authorization Header not coming across to WCF REST service
本文介绍了RestSharp - WCF REST 服务未遇到授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用基本身份验证通过 HTTPS 调用本地托管的 WCF REST 服务.
I am trying to call a locally hosted WCF REST service over HTTPS with basic auth.
这行得通,授权标头通过就好了,一切都很愉快:
This works and the Authorization header comes thru just fine and all is happy:
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
var request = (HttpWebRequest)WebRequest.Create("https://localhost/MyService/MyService.svc/");
request.Method = "GET";
request.ContentType = "application/json";
request.Headers.Add(
System.Net.HttpRequestHeader.Authorization,
"Basic " + this.EncodeBasicAuthenticationCredentials("UserA", "123"));
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream())
{
if (webStream != null)
{
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
}
}
}
然而,当我尝试使用 RestSharp 时,授权标头永远不会出现在请求中:
When I try to use RestSharp however, the Authorization header never comes thru on the request:
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
var credentials = this.EncodeBasicAuthenticationCredentials("UserA", "123");
var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/");
var restRq = new RestSharp.RestRequest("/");
restRq.Method = Method.GET;
restRq.RootElement = "/";
restRq.AddHeader("Authorization", "Basic " + credentials);
var restRs = client.Execute(restRq);
我用 RestSharp 方法做错了什么?
What am i doing wrong with the RestSharp method?
我知道 AddHeader 方法有效,因为:
I know that the AddHeader method works because this:
restRq.AddHeader("Rum", "And Coke");
会通过,只有授权"似乎被删除/丢失了.
will come thru, only "Authorization" seems stripped out/missing.
推荐答案
不要手动"添加标题,请执行以下操作:
instead of adding the header 'manually' do the following:
var client = new RestSharp.RestClient("https://localhost/MyService/MyService.svc/");
client.Authenticator = new HttpBasicAuthenticator("UserA", "123");
这篇关于RestSharp - WCF REST 服务未遇到授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文