简体   繁体   English

无法使用C#和摘要身份验证连接到TLS Web服务,Firefox成功,而IE8失败

[英]Can't connect to a TLS web service using C# and digest authentication, Firefox succeeds where IE8 fails

I'm trying to connect to a web service using C# and digest authentication, but every time I got the 401 - Not Authorized error. 我正在尝试使用C#连接到Web服务并进行摘要身份验证,但是每次出现401 - Not Authorized错误时,我都会尝试。 But when I try to reach the service over Firefox, everything's OK. 但是,当我尝试通过Firefox访问该服务时,一切正常。 When I use IE8, my password is not accepted and I got a 401. 当我使用IE8时,我的密码不被接受,并且我得到了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/>");
        }

    }

You are using the wrong overload of CredentialCache.Add , you should use CredentialCache.Add(Uri, string, NetworkCredential) instead. 您使用了错误的CredentialCache.Add重载 ,而应该使用CredentialCache.Add(Uri, string, NetworkCredential) The first one (with the port number) is only for SMTP. 第一个(带有端口号)仅用于SMTP。

cache.Add(uri, "Digest", netCredential);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 摘要式身份验证不适用于IE8,Firefox和Chrome浏览器 - Digest authentication not working on IE8, Firefox and Chrome are fine 使用摘要身份验证消耗Web服务 - Consuming a web service using digest authentication Windows 8 Web身份验证代理无法连接到服务 - Windows 8 Web Authentication Broker can't connect to the service 来自web.config的连接字符串失败,其中相同的硬编码字符串成功(带有C#的ASP.NET) - connection string from web.config fails where identical hard-coded string succeeds (ASP.NET with C#) 无法通过 C# 连接到网站,但使用 Firefox - Can't connect to website through C#, but with Firefox Windows 8.1 WCF客户端无法通过BasicHttpBinding通过摘要身份验证连接到服务 - Windows 8.1 WCF client fails to connect to service with digest authentication over BasicHttpBinding Windows应用商店中使用HttpClient(C#)的摘要式身份验证 - Digest authentication in Windows Store app using HttpClient (C#) 无法连接托管在 Azure Web 应用程序上的安全 Asp.Net Core Web 套接字(使用 TLS) - Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS) UWP App无法通过摘要身份验证作为多部分/表单数据发布到Web服务 - UWP App fails to post to a web service as multipart/form-data with digest authentication Selenium 2 C# Webdriver - 为什么我不能在 IE8 中从一个输入移动到另一个输入? - Selenium 2 C# Webdriver - Why can't I move from one input to another in IE8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM