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

发布于 2024-08-19 18:06:28 字数 1659 浏览 8 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海风掠过北极光 2024-08-26 18:06:28

您正在使用CredentialCache.Add的错误重载< /a>,您应该使用 CredentialCache.Add(Uri, string, NetworkCredential) 相反。第一个(带有端口号)仅适用于 SMTP。

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

You are using the wrong overload of CredentialCache.Add, you should use CredentialCache.Add(Uri, string, NetworkCredential) instead. The first one (with the port number) is only for SMTP.

cache.Add(uri, "Digest", netCredential);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文