在前一篇文章中,我们是把.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)的代码示例:

  1. public static string FindPfxbyThubmprintinLinux(string thumbprint)
  2. {
  3. if (string.IsNullOrEmpty(thumbprint))
  4. return $"Certificate with thumbprint {thumbprint} was not found";
  5.  
  6. string finalPath = $"/var/ssl/private/{thumbprint}.p12";
  7. var bytes2 = File.ReadAllBytes(finalPath);
  8. var cert = new X509Certificate2(bytes2);
  9. return cert.ToString();
  10. }

注意:

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

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

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.FileProviders;
  3. using System.Security.Cryptography.X509Certificates;
  4.  
  5. var builder = WebApplication.CreateBuilder(args);
  6.  
  7. // Add services to the container.
  8.  
  9. var app = builder.Build();
  10.  
  11. // Configure the HTTP request pipeline.
  12.  
  13. app.UseHttpsRedirection();
  14.  
  15. app.UseStaticFiles(new StaticFileOptions()
  16. {
  17. FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Images")),
  18. RequestPath = new PathString("/Images")
  19. });
  20.  
  21. app.MapGet("/loadpfxbyname", ([FromQuery(Name = "name")] string filename, [FromQuery(Name = "pwd")] string pwd) =>
  22. {
  23. var content = pfxTesting.LoadPfx(filename, pwd);
  24. return content;
  25. });
  26.  
  27. app.MapGet("/loadpfx/{pwd}", (string pwd) =>
  28. {
  29.  
  30. var content = pfxTesting.LoadPfx(null, pwd);
  31. return content;
  32. });
  33.  
  34. app.MapGet("/findpfx/{certThumbprint}", (string certThumbprint) =>
  35. {
  36.  
  37. var content = pfxTesting.FindPfx(certThumbprint);
  38. return content;
  39. });
  40.  
  41. app.Run();
  42.  
  43. class pfxTesting
  44. {
  45. public static string LoadPfx(string? filename, string password = "")
  46. {
  47. try
  48. {
  49. if (filename == null) filename = "contoso.com.pfx";
  50.  
  51. var bytes = File.ReadAllBytes(filename);
  52. var cert = new X509Certificate2(bytes, password);
  53.  
  54. return cert.ToString();
  55. }
  56. catch (Exception ex)
  57. {
  58. return ex.Message;
  59. }
  60. }
  61.  
  62. public static string FindPfx(string certThumbprint = "")
  63. {
  64. try
  65. {
  66. bool validOnly = false;
  67. using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
  68. {
  69. certStore.Open(OpenFlags.ReadOnly);
  70.  
  71. X509Certificate2Collection certCollection = certStore.Certificates.Find(
  72. X509FindType.FindByThumbprint,
  73. // Replace below with your certificate's thumbprint
  74. certThumbprint,
  75. validOnly);
  76. // Get the first cert with the thumbprint
  77. X509Certificate2 cert = certCollection.OfType<X509Certificate2>().FirstOrDefault();
  78.  
  79. if (cert is null)
  80. return FindPfxbyThubmprintinLinux(certThumbprint);
  81. //throw new Exception($"Certificate with thumbprint {certThumbprint} was not found");
  82.  
  83. return cert.ToString();
  84.  
  85. }
  86. }
  87. catch (Exception ex) { return ex.Message; }
  88. }
  89.  
  90. public static string FindPfxbyThubmprintinLinux(string thumbprint)
  91. {
  92. if (string.IsNullOrEmpty(thumbprint))
  93. return $"Certificate with thumbprint {thumbprint} was not found";
  94.  
  95. string finalPath = $"/var/ssl/private/{thumbprint}.p12";
  96. var bytes2 = File.ReadAllBytes(finalPath);
  97. var cert = new X509Certificate2(bytes2);
  98. return cert.ToString();
  99. }
  100. }

参考资料

在 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. 使用 Docker 部署 instantbox 轻量级 Linux 系统

    1)instantbox 介绍 GitHub:https://github.com/instantbox/instantbox instantbox 是一款非常实用的项目,它能够让你在几秒内启动一个主 ...

  2. szfpga 详细:高云1N1开发板高云gowin软件使用教程

      1.概述 国产FPGA是最近几年起来的产品,具有性价比高特点.高云FPGA,大多用在LED,电机控制,PLC设备上. 高云1N1开发板采用GW1N-LV1QN48C6/I5 FPGA器件.具有低功 ...

  3. etcd 集群安装

    1.环境准备 下载安装包:https://github.com/etcd-io/etcd/releases/ 这里下载的安装包为:etcd-v3.5.9-linux-amd64.tar.gz,即我们当 ...

  4. 科普达人丨一图看懂阿里云ECS

    简介: 建议收藏  原文链接:https://click.aliyun.com/m/1000363154/ 本文为阿里云原创内容,未经允许不得转载.

  5. 用积木讲运维,这样的IT人太会了

    简介: 日志服务SLS提供数据采集.加工.分析.告警可视化与投递功能,为AIOps.大数据分析.运营服务.大数据安全等场景提供支撑,并能以搭积木的方式适配各类运维场景,辅助企业的IT决策.近日,日志服 ...

  6. Java Agent 踩坑之 appendToSystemClassLoaderSearch 问题

    简介: 从 Java Agent 报错开始,到 JVM 原理,到 glibc 线程安全,再到 pthread tls,逐步探究 Java Agent 诡异报错. 作者:鲁严波   从 Java Age ...

  7. 如何实现事务原子性?PolarDB原子性深度剖析

    简介: 在巍峨的数据库大厦体系中,查询优化器和事务体系是两堵重要的承重墙,二者是如此重要以至于整个数据库体系结构设计中大量的数据结构.机制和特性都是围绕着二者搭建起来的.他们一个负责如何更快的查询到数 ...

  8. Apache Flink 在汽车之家的应用与实践

    ​简介: 汽车之家如何基于 Flink 上线了 AutoStream 平台并持续打磨. 本文整理自汽车之家实时计算平台负责人邸星星在 Flink Forward Asia 2020 分享的议题< ...

  9. [FE] 推荐两个能全球访问的 CDN 前端资源仓库

    https://unpkg.com/ https://cdnjs.com/ 部分资源库的版本不全. 访问速度请自行评估. Link:https://www.cnblogs.com/farwish/p/ ...

  10. dotnet 警惕使用 StackTrace 加获取方法标记 Attribute 特性在 Release 下被内联

    大家都知道,在 dotnet 里的 Debug 下和 Release 下的一个最大的不同是在 Release 下开启了代码优化.启用代码优化,将会对生成的 IL 代码进行优化,同时优化后的 IL 也会 ...