azure - .Net Core MVC 中安全服务请求的双向 SSL X509 证书位置 - Azure 服务托管应用程序

标签 azure asp.net-core openssl ssl-certificate

我一直在使用 x509 证书,以便向某些数据服务发出安全请求。它们需要双向 SSL 身份验证,因此我已将带有私钥的“沙盒”证书 (.crt) 转换为受密码保护的 .p12 文件。

这是第一个问题:我应该将该 .p12 文件放在哪里,以便我的应用程序在部署到 Azure(使用 DevOps)后可以读取该文件,但仍安全存储?我可以使用 Azure Key Vault 吗?

第二个问题是,在我的开发环境中,我无法在发出请求后建立 SSL 绑定(bind)(使用 .p12 绝对路径):

这是我正在使用的代码:

void GetATMs()
    {
        string requestURL = "https://sandbox.api.visa.com/globalatmlocator/v1/localatms/atmsinquiry";
        string userId = "MyUserId";
        string password = "MyPassword";
        string p12certificatePath = "C:\\Code\\projects\\project\\\\Clients\\PaymentGateways\\Visa\\Certs\\TC_keyAndCertBundle.p12";
        string p12certificatePassword = "CertPassword";
        string postData = @"{""wsRequestHeaderV2"": { ""requestTs"": ""2018-11-06T03:16:18.000Z"", ""applicationId"": ""VATMLOC"", ""requestMessageId"": ""ICE01-001"", ""userId"": ""CDISIUserID"", ""userBid"": ""10000108"", ""correlationId"": ""909420141104053819418"" }, ""requestData"": { ""culture"": ""en-US"", ""distance"": ""20"", ""distanceUnit"": ""mi"", ""metaDataOptions"": 0, ""location"": { ""address"": null, ""placeName"": ""700 Arch St, Pittsburgh, PA 15212"", ""geocodes"": null }, ""options"": { ""range"": { ""start"": 10, ""count"": 20 }, ""sort"": { ""primary"": ""city"", ""direction"": ""asc"" }, ""operationName"": ""or"", ""findFilters"": [ { ""filterName"": ""OPER_HRS"", ""filterValue"": ""C"" } ], ""useFirstAmbiguous"": true } } }";

        HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
        request.Method = "POST";
        // Add headers
        string authString = userId + ":" + password;
        var authStringBytes = System.Text.Encoding.UTF8.GetBytes(authString);
        string authHeaderString = Convert.ToBase64String(authStringBytes);
        request.Headers["Authorization"] = "Basic " + authHeaderString;
        // Add certificate
        var certificate = new X509Certificate2(p12certificatePath, p12certificatePassword);

        request.ClientCertificates.Add(certificate);
        request.Accept = "application/json";
        var data = Encoding.ASCII.GetBytes(postData);
        request.ContentLength = data.Length;
        // Get the request stream.  
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.  
        dataStream.Write(data, 0, data.Length);
        // Close the Stream object.  
        dataStream.Close();
        // Get the response.  
        WebResponse response = request.GetResponse();
        // Display the status.  
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.  
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.  
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.  
        string responseFromServer = reader.ReadToEnd();
        // Display the content.  
        Console.WriteLine(responseFromServer);
        // Clean up the streams.  
        reader.Close();
        dataStream.Close();
        response.Close();

我在这里缺少什么?

它以下列方式失败:

An unhandled exception occurred while processing the request.
Win32Exception: The credentials supplied to the package were not recognized
System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface secModule, string package, CredentialUse intent, SCHANNEL_CRED scc)

HttpRequestException: The SSL connection could not be established, see inner exception.
System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)

WebException: The SSL connection could not be established, see inner exception. The credentials supplied to the package were not recognized
System.Net.HttpWebRequest.GetResponse()

我们的域有通配符 SSL。它们有什么不同吗?它是否可以在 Visa 仪表板中注册并用于发出安全请求,因为它是由受信任的 CA 机构签名的?

最佳答案

嗯,是的。根据 @dagope 建议,我已将证书上传到 Azure 上的 key 管理并通过 SDK 访问它。这也是 Azure 上 key /证书管理的最佳实践。

关于azure - .Net Core MVC 中安全服务请求的双向 SSL X509 证书位置 - Azure 服务托管应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53172332/

相关文章:

Azure kusto集群表通过数据列实现行级安全性,基于哪些数据列可以跨多个表进行过滤?

c# - UseExceptionHandler 不会捕获任务异常?

nginx - CentOS 安装 nginx 失败

security - 从 p12 文件中导出公钥

node.js - AD 授权组更改未反射(reflect)

azure - 无法删除启用版本控制的 Azure 存储帐户

asp.net-core - 在布局中访问 .Net Core 部分 View 中的数据的 "right way"是什么?

c# - AWS .NET SDK 使用 ASP.NET Core 依赖注入(inject)配置 S3 客户端

ssl - 更改自签名证书的主题属性

amazon-web-services - 将子域从 aws 指向 azure。可能的?