在前一篇文章中,我们是把.NET 8应用读取SSL证书(X509)示例部署在App Service Windows环境中,那么如果部署在Linux环境,以及Linux Container中呢?

根据前文中的第一种方法,直接在把证书文件包含在源文件中,通过相对路径读取证书文件的方式,经测试,可以正常工作。

但是,对于第二种“通过指纹在系统证书库中查找证书 ”的方式,在Linux系统中,是不能使用 X509Store(StoreName.My, StoreLocation.CurrentUser) 中查找的方式。

经过测试验证,在App Service Linux( 包含Linux Container)证书页面上传的证书后,系统会把证书保存为文件。存储在 /var/ssl/ 文件夹中,可以通过ssh 方式查看:

  1. 进入App Service Kudu(高级工具)页面: https://<yourwebappname>.scm.chinacloudsites.cn/webssh/host
  2. 点击SSH目录,输入cd 目录命令: cd /var/ssl/private 后,列举全部文件: ls -ll

在.NET 8代码中的正确读取私有证书 (.pfx)的代码示例:

    public static string FindPfxbyThubmprintinLinux(string thumbprint)
{
if (string.IsNullOrEmpty(thumbprint))
return $"Certificate with thumbprint {thumbprint} was not found"; string finalPath = $"/var/ssl/private/{thumbprint}.p12";
var bytes2 = File.ReadAllBytes(finalPath);
var cert = new X509Certificate2(bytes2);
return cert.ToString();
}

注意:

  • WEBSITE_LOAD_CERTIFICATES  配置不可少
  • 门户上的证书添加后,需要重启站点,等待实例中出现证书文件。(通常在15分钟左右后才能在目录中看见 thumbprint.p12文件)

附录:示例代码(.NET 8.0 顶级语句 program.cs)

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using System.Security.Cryptography.X509Certificates; var builder = WebApplication.CreateBuilder(args); // Add services to the container. var app = builder.Build(); // Configure the HTTP request pipeline. app.UseHttpsRedirection(); app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Images")),
RequestPath = new PathString("/Images")
}); app.MapGet("/loadpfxbyname", ([FromQuery(Name = "name")] string filename, [FromQuery(Name = "pwd")] string pwd) =>
{
var content = pfxTesting.LoadPfx(filename, pwd);
return content;
}); app.MapGet("/loadpfx/{pwd}", (string pwd) =>
{ var content = pfxTesting.LoadPfx(null, pwd);
return content;
}); app.MapGet("/findpfx/{certThumbprint}", (string certThumbprint) =>
{ var content = pfxTesting.FindPfx(certThumbprint);
return content;
}); app.Run(); class pfxTesting
{
public static string LoadPfx(string? filename, string password = "")
{
try
{
if (filename == null) filename = "contoso.com.pfx"; var bytes = File.ReadAllBytes(filename);
var cert = new X509Certificate2(bytes, password); return cert.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
} public static string FindPfx(string certThumbprint = "")
{
try
{
bool validOnly = false;
using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
certStore.Open(OpenFlags.ReadOnly); X509Certificate2Collection certCollection = certStore.Certificates.Find(
X509FindType.FindByThumbprint,
// Replace below with your certificate's thumbprint
certThumbprint,
validOnly);
// Get the first cert with the thumbprint
X509Certificate2 cert = certCollection.OfType<X509Certificate2>().FirstOrDefault(); if (cert is null)
return FindPfxbyThubmprintinLinux(certThumbprint);
//throw new Exception($"Certificate with thumbprint {certThumbprint} was not found"); return cert.ToString(); }
}
catch (Exception ex) { return ex.Message; }
} public static string FindPfxbyThubmprintinLinux(string thumbprint)
{
if (string.IsNullOrEmpty(thumbprint))
return $"Certificate with thumbprint {thumbprint} was not found"; string finalPath = $"/var/ssl/private/{thumbprint}.p12";
var bytes2 = File.ReadAllBytes(finalPath);
var cert = new X509Certificate2(bytes2);
return cert.ToString();
}
}

参考资料

在 Linux/Windows 容器中加载证书 : https://docs.azure.cn/zh-cn/app-service/configure-ssl-certificate-in-code#load-certificate-in-linuxwindows-containers

GetX509CertificateLinux(string thumbprint)  :

https://learn.microsoft.com/en-us/answers/questions/1055731/application-error-on-linux-running-net-core

Load Certificate on Linux Web App #19305 : https://github.com/MicrosoftDocs/azure-docs/issues/19305

【END】

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)的更多相关文章

  1. 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)

    关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...

  2. 在 Azure 中的 Linux 虚拟机上使用 SSL 证书保护 Web 服务器

    若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Linux 虚 ...

  3. 在 Azure 中的 Windows 虚拟机上使用 SSL 证书保护 IIS Web 服务器

    若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Windows ...

  4. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  5. Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...

  6. 搭建DAO层和Service层代码

    第一部分建立实体和映射文件 1 通过数据库生成的实体,此步骤跳过,关于如何查看生成反向工程实体类查看SSH框架搭建教程-反向工程章节 Tmenu和AbstractorTmenu是按照数据库表反向工程形 ...

  7. Kivy A to Z -- 怎样从python代码中直接訪问Android的Service

    在Kivy中,通过pyjnius扩展能够间接调用Java代码,而pyjnius利用的是Java的反射机制.可是在Python对象和Java对象中转来转去总让人感觉到十分别扭.好在android提供了b ...

  8. Azure AD Domain Service(二)为域服务中的机器配置 Azure File Share 磁盘共享

    一,引言 Azure File Share 是支持两种认证方式的! 1)Active Directory 2)Storage account key 记得上次分析的 "Azure File ...

  9. Express4.10.2开发框架中默认app.js的代码注释

    //通过require()加载了express.path等模块var express = require('express');var path = require('path');var favic ...

  10. 【Azure 存储服务】代码版 Azure Storage Blob 生成 SAS (Shared Access Signature: 共享访问签名)

    问题描述 在使用Azure存储服务,为了有效的保护Storage的Access Keys.可以使用另一种授权方式访问资源(Shared Access Signature: 共享访问签名), 它的好处可 ...

随机推荐

  1. 记录如何用php做一个网站访问计数器的方法

    简介创建一个简单的网站访问计数器涉及到几个步骤,包括创建一个用于存储访问次数的文件或数据库表,以及编写PHP脚本来增加计数和显示当前的访问次数. 方法以下是使用文件存储访问次数的基本步骤: 创建一个文 ...

  2. 力扣539(java)-最小时间差(中等)

    题目: 给定一个 24 小时制(小时:分钟 "HH:MM")的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示. 示例 1: 输入:timePoints = [" ...

  3. 安全同学讲Maven间接依赖场景的仲裁机制

    简介: 去年的Log4j-core的安全问题,再次把供应链安全推向了高潮.在供应链安全的场景,蚂蚁集团在静态代码扫描平台-STC和资产威胁透视平台-哈勃这2款产品在联合合作下,优势互补,很好的解决了直 ...

  4. 如何用一个插件解决 Serverless 灰度发布难题?

    简介: 我们可以发现相比使用控制台进行灰度发布,使用 FC-Canary 插件免去了用户手动创建版本.发布别名.关联触发器和管理自定义域名的麻烦,使用起来非常方便. 作者:长淇 导读 本文适合: 想了 ...

  5. UWP 从文件 StorageFile 转 SoftwareBitmap 图片方法

    本文告诉大家如何在 UWP 从 文件 StorageFile 转 SoftwareBitmap 图片的方法 使用以下三步即可从文件 StorageFile 转 SoftwareBitmap 图片 第一 ...

  6. 2019-9-19-dotnet-找不到-PostAsJsonAsync-方法

    title author date CreateTime categories dotnet 找不到 PostAsJsonAsync 方法 lindexi 2019-09-19 14:53:58 +0 ...

  7. Dubbo SPI-Wrapper

    前言 在Dubbo SPI中是通过Wrapper实现AOP,对于AOP相信大家都不陌生,这里不做的过多的介绍,我们主要来了解Dubbo SPI中是如何使用Wrapper类以及实现的细节. 使用场景 D ...

  8. .Net 8.0 下的新RPC,IceRPC之"请求"生命线意义非凡

    作者引言 很高兴啊,我们来到了IceRPC之"请求"生命线意义非凡,号称"死亡时间"的追命线,颤抖吧! "请求"生命线之意义非凡 本文将深入 ...

  9. C#使用MX Component实现三菱PLC软元件数据采集的完整步骤(仿真)

    前言 本文介绍了如何使用三菱提供的MX Component插件实现对三菱PLC软元件数据的读写,记录了使用计算机仿真,模拟PLC,直至完成测试的详细流程,并重点介绍了在这个过程中的易错点,供参考. 用 ...

  10. 使用小波分析和深度学习对心电图 (ECG) 进行分类 mcu-ai低成本方案 mcu-ai低成本方案

    具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI人工智能 此示例说明如何使用连续小波变换 (CWT) 和深度卷积神经网络 (CNN) 对人体心电图 (EC ...