【问题标题】:.NET Mutual SSL handshake 'Client Authentication'.NET 相互 SSL 握手“客户端身份验证”
【发布时间】:2014-04-14 07:43:32
【问题描述】:

过去几天我一直在解决这个问题,但我没有得到任何结果。

场景是:

该领域的 iOS 应用程序将调用我的 REST 服务 (.NET)。 我的 REST 服务将使用相互 SSL 握手来调用 Apache Web 服务。 无论我收到什么数据,我都必须将其传回现场的 iOS 设备。

唯一的问题是 My REST Service 和 Apache Web 服务之间的第二部分通信。

客户端证书已使用客户端身份验证、数字证书、密钥加密等密钥用法进行签名。我的证书的根签名者已放置在 Apache 服务器上。如果我们尝试使用 Web 浏览器,我们可以毫无问题地执行握手。

我用于使用 SslStream 执行身份验证的示例代码。使用这种方法我得到一个错误说明

The message received was unexpected or badly formatted

管理 Apache Web 服务器的人说他们可以看到请求进入,但据他们说他们没有收到证书。

var certificate = @“certificate.cer”;
var hostAddress = “hostAddress";
var certificates = new X509Certificate2Collection(new X509Certificate2(certificate));
RunClient(hostAddress, 1316, certificates);

static void RunClient(string hostName, int port, X509Certificate2Collection certificates)
{
    TcpClient client = new TcpClient(hostName, port);
    SslStream sslStream = new SslStream(client.GetStream(), false, ValidateServerCertificate);
    try
    {
        sslStream.AuthenticateAsClient(hostName, certificates, SslProtocols.Ssl3, true);
        Write("authenticated.");
    }
    catch (AuthenticationException ex)
    {
        Write("Inner: " + ex.InnerException.Message);                
    }
    catch (Exception ex)
    {
        Write(ex.Message);
    }
}

我使用 HttpWebRequest 执行身份验证的示例代码。使用这种方法会给我以下问题

The request was aborted: Could not create SSL/TLS secure channel.

var certificate = @“certificate.cer”;
var hostAddress = “hostAddress";
var certificates = new X509Certificate2Collection(new X509Certificate2(certificate));
public static T Post(string url, string body, X509Certificate2 cert)
{
    var webRequest = FBJsonRequestService.CreateRequest(url, cert, WebRequestMethods.Http.Post, body);
    using (var webResponse = webRequest.GetResponse())
    {
        return CreateResponse(webResponse);
    }
}
var webRequest = (HttpWebRequest) WebRequest.Create(url);
webRequest.ClientCertificates.Add(cert);
//webRequest.AuthenticationLevel = AuthenticationLevel.MutualAuthRequested;
webRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
webRequest.Method = method;
webRequest.ContentType = "application/json; charset=utf-8";
if (body != null)
{
    using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
    {
        streamWriter.Write(body);
    }
}
return webRequest;

我希望这是有道理的。我只想知道我做的是对还是错。

【问题讨论】:

    标签: c# .net apache ssl authentication


    【解决方案1】:

    “请求被中止:无法创建 SSL/TLS 安全通道”异常的另一个原因 是服务器可能不支持安全协议类型。

    另一种协议类型是 TLS,它比 SSL3 更安全, 下面的代码可以帮助你

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
    

    【讨论】:

      【解决方案2】:

      谢谢佩波,

      我发现我使用了错误的文件。我不得不使用 PFX 文件而不是 CER。我的问题结果完全是另外一回事。这是证书本身的一些安全限制。在发布此之前,我什至不确定我是否做对了。

      我为将来需要任何帮助的人创建了以下blog

      【讨论】:

        【解决方案3】:

        我认为文件 certificate.cer 的内容不正确。我认为扩展名 .cer 仅包含证书,但不包含私钥。

        您必须使用包含私钥的 certificate.p12certificate.pfx。这些扩展代表 PKCS#12 标准。这些文件中还可以包含整个证书链。

        您可以使用 X509Certificate2 类的不同构造函数来加载 p12 文件。请看this documentation

        【讨论】:

          猜你喜欢
          • 2014-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多