ASP.NET HTTP认证头 [英] ASP.NET HTTP Authorization Header
问题描述
我想知道为什么,当它被命名为'授权',但将正常工作,当我改变一个字符,说授权我的asp.net应用程序将不会在头添加到我的职务。在其他网站的文档他们总是使用名称授权,所以我想,以及在这一点上我只想下架原因。
I would like to know why my asp.net application will not add the header to my post when it is named 'Authorization' but will work fine when I change one character, say "Authorizations". In documentation for other sites they always use the name "Authorization" so I would like to as well and at this point I just want to under stand why.
我看了一下这几个主题,但没有发现任何逻辑的原因。
I have read a few topics about this but have not found any logical reason why.
下面是我的code以下:
Here is my code below:
string fileName = "c:\\xyz.xml";
string uri = "http://myserver/Default.aspx";
req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
byte[] authBytes = Encoding.UTF8.GetBytes("DDSServices:jCole2011".ToCharArray());
req.Headers.Add("Authorization", "BASIC " + Convert.ToBase64String(authBytes) );
req.Headers.Add("test", "test");
UTF8Encoding encoder = new UTF8Encoding();
byte[] data = encoder.GetBytes(this.GetTextFromXMLFile(fileName));
req.ContentLength = data.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
req.Headers.Add("Authorization", "BASIC" + Convert.ToBase64String(authBytes));
System.Net.WebResponse response = req.GetResponse();
System.IO.StreamReader reader = new StreamReader(response.GetResponseStream());
string str = reader.ReadToEnd();
另一个恼人这是当我通过添加小提琴手在观察的变量,它工作正常。
The other annoying this is when i add the watched variable through fiddler it works fine.
推荐答案
我遇到了一个问题,如何认证/证书添加到标头。我发现以下面的方式将溶液
I was ran into a question how to add Authentication/Credentials to the headers. I found the solution in the following way.
string _auth = string.Format("{0}:{1}", "myUser","myPwd");
string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
string _cred = string.Format("{0} {1}", "Basic", _enc);
req.Headers[HttpRequestHeader.Authorization] = _cred;
这给了我我想这些头(粘贴Wireshark的描述),
Which gave me those headers I want (pasted Wireshark descriptions),
授权:基本bXlVc2VyOm15UHdk \\ r \\ n结果
凭据:MYUSER:MYPWD
Authorization: Basic bXlVc2VyOm15UHdk\r\n
Credentials: myUser:myPwd
这篇关于ASP.NET HTTP认证头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!