C# RestSharp - WCF REST 服务未遇到授权标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17762523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RestSharp - Authorization Header not coming across to WCF REST service
提问by Chris Hawkins
I am trying to call a locally hosted WCF REST service over HTTPS with basic auth.
我正在尝试使用基本身份验证通过 HTTPS 调用本地托管的 WCF REST 服务。
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();
}
}
}
When I try to use RestSharp however, the Authorization header never comes thru on the request:
但是,当我尝试使用 RestSharp 时,请求中永远不会出现 Authorization 标头:
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);
What am i doing wrong with the RestSharp method?
我用 RestSharp 方法做错了什么?
I know that the AddHeader method works because this:
我知道 AddHeader 方法有效,因为:
restRq.AddHeader("Rum", "And Coke");
will come thru, only "Authorization" seems stripped out/missing.
会通过,只有“授权”似乎被删除/丢失。
采纳答案by wal
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");
回答by milanseitler
You have to use ParameterType.HttpHeaderparameter:
您必须使用ParameterType.HttpHeader参数:
request.AddParameter("Authorization", "data", ParameterType.HttpHeader);
回答by j.hull
I used milano's answer to get my REST service call to work (using GET)
我使用 milano 的答案让我的 REST 服务调用工作(使用 GET)
Dim client2 As RestClient = New RestClient("https://api.clever.com")
Dim request2 As RestRequest = New RestRequest("me", Method.GET)
request2.AddParameter("Authorization", "Bearer " & j.access_token, ParameterType.HttpHeader)
Dim response2 As IRestResponse = client2.Execute(request2)
Response.Write("** " & response2.StatusCode & "|" & response2.Content & " **")
The key was making sure there was a space after the word 'Bearer' but this may apply to any type of custom token authorization header
关键是确保“Bearer”一词后面有一个空格,但这可能适用于任何类型的自定义令牌授权标头