【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)

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

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

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

经过测试验证,在App Service Linux( 包含Linux Container)证书页面上传的证书后,系统会把证书保存为文件。存储在 /var/ssl/ 文件夹中,可以通过ssh 方式查看:
- 进入App Service Kudu(高级工具)页面: https://<yourwebappname>.scm.chinacloudsites.cn/webssh/host
- 点击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) :
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)的更多相关文章
- 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)
关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...
- 在 Azure 中的 Linux 虚拟机上使用 SSL 证书保护 Web 服务器
若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Linux 虚 ...
- 在 Azure 中的 Windows 虚拟机上使用 SSL 证书保护 IIS Web 服务器
若要保护 Web 服务器,可以使用安全套接字层 (SSL) 证书来加密 Web 流量. 这些 SSL 证书可存储在 Azure Key Vault 中,并可安全部署到 Azure 中的 Windows ...
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fanc ...
- 搭建DAO层和Service层代码
第一部分建立实体和映射文件 1 通过数据库生成的实体,此步骤跳过,关于如何查看生成反向工程实体类查看SSH框架搭建教程-反向工程章节 Tmenu和AbstractorTmenu是按照数据库表反向工程形 ...
- Kivy A to Z -- 怎样从python代码中直接訪问Android的Service
在Kivy中,通过pyjnius扩展能够间接调用Java代码,而pyjnius利用的是Java的反射机制.可是在Python对象和Java对象中转来转去总让人感觉到十分别扭.好在android提供了b ...
- Azure AD Domain Service(二)为域服务中的机器配置 Azure File Share 磁盘共享
一,引言 Azure File Share 是支持两种认证方式的! 1)Active Directory 2)Storage account key 记得上次分析的 "Azure File ...
- Express4.10.2开发框架中默认app.js的代码注释
//通过require()加载了express.path等模块var express = require('express');var path = require('path');var favic ...
- 【Azure 存储服务】代码版 Azure Storage Blob 生成 SAS (Shared Access Signature: 共享访问签名)
问题描述 在使用Azure存储服务,为了有效的保护Storage的Access Keys.可以使用另一种授权方式访问资源(Shared Access Signature: 共享访问签名), 它的好处可 ...
随机推荐
- MySQL组合索引
MySQL组引合索优化SQL 我的场景 200w左右的数据,后面会更多 使用定时任务爬取数据插入到自己的数据库.要保证数据的唯一性,所以我用了组合唯一索引. 表结构 最初的组合索引 SQL执行和exp ...
- 剑指offer03(Java)-数组中重复的数字(简单)
题目: 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任 ...
- 我们为什么要做 SoloPi
SoloPi现状 去年(2019年)7月份,蚂蚁集团正式对外开源了客户端自动化测试工具 SoloPi ,其主要包括三大模块:录制回放(用于功能测试).性能工具(用于性能测试)以及一机多控(服务于兼容性 ...
- 斩获大奖|阿里云PolarDB-X引领云原生分布式数据库新时代
简介:阿里云原生分布式数据库PolarDB-X荣获"2021年度最佳分布式数据库". 12月15-16日,以"引领分布式云变革 助力湾区数字经济"为主题的全球分 ...
- 不改一行代码,轻松拥有企业级微服务治理|MSE微服务治理专业版重磅发布
简介:随着业务的发展,微服务拆分越来越复杂,微服务的治理也成了一个比较令人头疼的问题.有没有更加简单且高效的方法来解决微服务治理的难题? 作者:十眠 随着业务的发展,微服务拆分越来越复杂,微服务的治 ...
- [PHP] 业务逻辑大内存占用的优化思路, yield 和 chunk
示例: header("content-type:text/html;charset=utf-8"); function readTxt() { $handle = fopen ...
- 聊聊 dotnet 7 对 bool 与字符串互转的底层性能优化
本文也叫 跟着 Stephen Toub 大佬学性能优化系列.大家都知道在 .NET 7 有众多的性能优化,其中就包括了对布尔和字符串互转的性能优化.在对布尔和字符串的转换的性能优化上,有着非常巧妙的 ...
- dotnet C# 获取当前设备可移动磁盘
本文告诉大家如何获取当前设备的可移动磁盘 在我的 WPF 应用里面,期望获取到 U 盘的所在盘进行一些有趣的逻辑.可以通过 DriveInfo 类的 GetDrives 获取当前所有的驱动器磁盘 再通 ...
- 和 ChatGPT 聊聊 .NET 编译和执行背后的那些事儿
1 .NET 编译.构建.执行涉及到哪些概念 在 .NET 编译.构建和执行中,涉及到以下概念: C# 或 Visual Basic .NET 等编程语言: 这些是 .NET Framework 使用 ...
- STM32 ADC使用问题
基本信息 MCU:STM32F105R8T6 库:HAL 平台:MDK 精度:12位 问题一 现象: 在测量的时候,发现采样值在 1023 ~ 1042 和 1279 ~ 1290 两个区间之间无法测 ...