ASP.NET Core Web Api配置Azure Key Vault
2021-02-07 04:18
标签:star tin ring app focus absolute rtu ack ros 1.1 安装 下载.NETCORE SDK 进行安装 下载NETCORE RUNTIME进行安装. 下载Runtime & Hosting Bundle进行安装 下载地址:https://dotnet.microsoft.com/download/dotnet-core 微软的安装在windows方面很简单,基本就是一键安装. 1.2 开发工具 VisualStudio 2019 1.3建立项目工程 点击文件创建项目,出现如下图示 选择画红圈部分,接着出现如下图示 2.1 将应用程序与 Azure Active Directory 集成 2.1.1登录Azure protal , 选择Azure Active Directory 网址:https://portal.azure.cn 2.1.2 选择应用注册 2.1.3 点击添加新注册,输入名称点确定,即会自动生成客户端ID 2.2 创建key vault 2.3 创建机密 3.1 安装nuget依赖包 3.2 代码配置 3.3 使用key vault 其中 “AppSecret”即是key vault中设置的键,通过_configuration["AppSecret"]可以获取到值。 ASP.NET Core Web Api配置Azure Key Vault 标签:star tin ring app focus absolute rtu ack ros 原文地址:https://www.cnblogs.com/zmsoftbj/p/11401922.html
1. 项目创建
2. Azure Key Vault
3. 代码配置 Key Vault
Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, builder) =>
{
var keyVaultEndpoint = GetKeyVaultEndpoint();
if (!string.IsNullOrEmpty(keyVaultEndpoint))
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
builder.AddAzureKeyVault(
keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());
}
}
).UseStartup
public class AboutModel : PageModel
{
public string Message { get; set; }
public AboutModel(IConfiguration configuration)
{
_configuration = configuration;
}
private readonly IConfiguration _configuration = null;
public void OnGet()
{
Message = "My key val = " + _configuration["AppSecret"];
}
}
文章标题:ASP.NET Core Web Api配置Azure Key Vault
文章链接:http://soscw.com/index.php/essay/52026.html