无法使用 C# 和摘要身份验证连接到 TLS Web 服务,Firefox 成功,而 IE8 失败
我尝试使用 C# 和摘要身份验证连接到 Web 服务,但每次都会收到 401 - Not Authorized
错误。但当我尝试通过 Firefox 访问该服务时,一切正常。当我使用 IE8 时,我的密码不被接受,并且出现 401。
您有什么想法吗?感谢您的帮助。
这是我正在使用的测试代码:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback
= delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
Uri uri = new Uri(URL);
NetworkCredential netCredential = new NetworkCredential(username, password);
CredentialCache cache = new CredentialCache();
cache.Add(URL, 443, "Digest", netCredential);
WebRequest request = WebRequest.Create(URL);
request.Credentials = cache;
request.PreAuthenticate = true;
request.Method = "POST";
WebResponse response;
try
{
response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
Response.Write(result);
response.Close();
reader.Close();
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message + "<br/><br/><br/>");
Response.Write("Request Headers<br/><br/>");
WebHeaderCollection headers = request.Headers;
// Get each header and display each value.
foreach (string key in headers.AllKeys)
{
string value = headers[key];
Response.Write(key + ": " + value);
Response.Write("<br/><br/>");
}
}
I'm trying to connect to a web service using C# and digest authentication, but every time I got the 401 - Not Authorized
error. But when I try to reach the service over Firefox, everything's OK. When I use IE8, my password is not accepted and I got a 401.
Do you have any ideas? Thanks for the help.
Here's the test code I'm using:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback
= delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
Uri uri = new Uri(URL);
NetworkCredential netCredential = new NetworkCredential(username, password);
CredentialCache cache = new CredentialCache();
cache.Add(URL, 443, "Digest", netCredential);
WebRequest request = WebRequest.Create(URL);
request.Credentials = cache;
request.PreAuthenticate = true;
request.Method = "POST";
WebResponse response;
try
{
response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string result = reader.ReadToEnd();
Response.Write(result);
response.Close();
reader.Close();
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message + "<br/><br/><br/>");
Response.Write("Request Headers<br/><br/>");
WebHeaderCollection headers = request.Headers;
// Get each header and display each value.
foreach (string key in headers.AllKeys)
{
string value = headers[key];
Response.Write(key + ": " + value);
Response.Write("<br/><br/>");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
CredentialCache.Add
的错误重载< /a>,您应该使用CredentialCache.Add(Uri, string, NetworkCredential)
相反。第一个(带有端口号)仅适用于 SMTP。You are using the wrong overload of
CredentialCache.Add
, you should useCredentialCache.Add(Uri, string, NetworkCredential)
instead. The first one (with the port number) is only for SMTP.