在HTTP请求的授权标头中获取已签名的OAuth请求c#

时间:2017-07-20 02:06:58

标签: c# oauth httpwebrequest oauth-1.0a

我使用以下代码从网址获取授权标头,但授权字段始终返回为空

 string url = "https://xyz.appdirect.com/api/integration/v1/events/abc-123";
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            string value = httpWebRequest.Headers["Authorization"];

当我按照MSDN代码时,我得到了标题

的空值
  WebRequest request = WebRequest.Create(url);
            request.Method = WebRequestMethods.Http.Get;
            NameValueCollection authHeader = request.Headers;

            if (authHeader.Count > 0)
            {
                foreach (string strKey in authHeader)
                {
                    string s = strKey + " = " + request.Headers[strKey] + "<br />\n";
                    Console.WriteLine(String.Format(" Key Value header: {0}", authHeader[strKey]));

                }
            }
            else
            {
                Console.WriteLine(String.Format("No headers found"));

            }

获取授权标头的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

简单的解决方案是从HttpContext获取标题,但我将其复制到其他内容而不是直接从HttpContext读取。

NameValueCollection authHeader = HttpContext.Current.Request.Headers;
foreach (string strKey in authHeader)
{
    _logger.Info(String.Format(" Key Value header: {0}", authHeader[strKey]));
}