【问题标题】:Two-way authentication using ssl in dotnet在 dotnet 中使用 ssl 进行双向身份验证
【发布时间】:2011-05-25 13:55:10
【问题描述】:

我有一个项目需要通过网络请求发送数据文件。我们需要设置双向身份验证,也称为相互身份验证。我们不确定是否需要特殊证书,但我们知道它需要达到 3 级。

我无法找到此案例的示例代码。我不知道在哪里添加我们的证书信息。使用此代码,当我们尝试读取响应流并且永远不会调用 ServicePointManager.ServerCertificateValidationCallback 时,会引发 Underlying connection is closed 错误。这是我所拥有的:

ServicePointManager.ServerCertificateValidationCallback = New Security.RemoteCertificateValidationCallback(AddressOf MyCertValidationCb)
            httpReq = CType(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
            For Each cert As String In certs
                X509cert = X509Certificate2.CreateFromCertFile(cert)
                X509cert2 = New X509Certificate2(X509cert)
                httpReq.ClientCertificates.Add(X509cert2)
            Next
            httpReq.Method = "POST"        ' Post method
            httpReq.ContentType = "text/xml"               ' content type

            ' Wrap the request stream with a text-based writer
            writer = New StreamWriter(httpReq.GetRequestStream())
            ' Write the XML text into the stream
            reader = New StreamReader(filename.Name)
            ret = reader.ReadToEnd()
            reader.Close()
            ' Send the data to the webserver
            writer.WriteLine(ret)
            writer.Close()
            ' Wait for response
            Dim httpRsp As System.Net.HttpWebResponse = CType(httpReq.GetResponse(), HttpWebResponse)
            sr = New StreamReader(httpRsp.GetResponseStream)
            responseText = sr.ReadToEnd

            If httpReq IsNot Nothing Then
                httpReq.GetRequestStream().Close()
            End If
            If httpRsp IsNot Nothing Then
                httpRsp.GetResponseStream().Close()
            End If

任何带有示例代码的博客提示或链接都会很棒。 谢谢。

【问题讨论】:

  • 您是否有阻止在 SOAP 上使用 WS-Security 的特殊要求?在 .NET 和 IIS(即 WCF)之间,这将使用共享证书处理 SSL 传输和拒绝。切换到 TLS 和/或消息加密是设置标志的问题。那么不需要 Class 3,只需一个 Class 1 PKCS12,带有私钥,不需要受信任的链根,就可以工作。

标签: vb.net authentication ssl-certificate two-way ssl-security


【解决方案1】:

您不需要“特殊”证书。您的客户端需要自己的证书并在连接中使用它来告诉服务器它的身份。这称为客户端证书。服务器应该正确处理这个问题。

以下 MSDN 文章讨论了如何设置您的 ClientCertificate: http://msdn.microsoft.com/en-us/library/ms732391.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

【讨论】:

    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多