c# - .NET 相互 SSL 握手 'Client Authentication'

标签 c# .net apache ssl authentication

过去几天我一直在研究这个问题,但我一无所获。

场景是:

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

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

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

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

收到的消息是意外的或格式错误

管理 Apache 网络服务器的人说他们可以看到收到的请求,但据他们说他们没有收到带有此证书的证书。

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 执行身份验证的示例代码。使用此方法会出现以下问题

请求被中止:无法创建 SSL/TLS 安全通道。

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;

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

最佳答案

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

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

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

关于c# - .NET 相互 SSL 握手 'Client Authentication',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313138/

相关文章:

c# - 使用 C# 获取 Umbraco 选项卡

.net - Redis 用于频繁更改的市场信息

c# - 如何在正则表达式中使用变量?

c# - 是这个值对象

c# - 签名验证失败。无法匹配 'kid'

c# - 如何删除字符 8236

c# - 使用 Moq 和 C# 调用作为参数传递给模拟的 Func

php - url 中的 htaccess rewriterule 2 参数

php - HTTP 认证 PHP 和 HTML 表单

html - CSS 背景图像 : url(none) leading to errors in apache log file