《Windows Azure Platform 系列文章目录

  本章我们介绍如何在Azure Windows VM里面,使用.NET使用Azure Key Vault

  我们需要对Key Vault进行身份验证,所以需要提供凭据。因此,在启动过程中,这是一个难以兼顾的典型问题。 托管服务标识 (MSI) 提供简化该过程的启动标识,可以解决此问题。

  为 Azure 服务(例如 Azure 虚拟机、Azure 应用服务或 Azure Functions)启用 MSI 时,Azure 会创建一个服务主体。 MSI 针对 Azure Active Directory (Azure AD) 中的服务实例提供启动标识,并将服务主体凭据注入该实例。

  

    

  我们需要准备以下内容:

  1.创建一个资源组,命名为:keyvault-rg

  2.创建1个Key Vault,并创建Secret Vaule

  3.创建1台Windows VM,在这台Windows VM安装Visual Studio (这台VM必须与KeyVault在同一个资源组)

  4.安装并运行Azure CLI

az cloud set --name AzureChinaCloud
az login

  5.在弹出的输入框中,输入Azure China的用户名和密码

  6.如果我们有多个订阅,首先需要选择订阅:

az account set --subscription 订阅ID

  7.为VM分配Identity

az vm identity assign --name VMName --resource-group VM所在的资源组名称

  8.执行完毕后,显示的结果如下:

  我们把systemAssignedIdentity后面的信息保存下来

{
"systemAssignedIdentity": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"userAssignedIdentities": {}
}

  

  9.为VM分配权限

  我们执行下面的命令:

az keyvault set-policy --name VM的名称 --object-id 步骤8中显示的systemAssignedIdentity值 --secret-permissions get list

  10.登录到虚拟机,创建1个Windows Console Project。选择NuGet,增加Newtonsoft Package

  11.增加如下的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.IO; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Step 1: Get a token from the local (URI) Managed Service Identity endpoint, which in turn fetches it from Azure AD
var token = GetToken(); // Step 2: Fetch the secret value from your key vault
System.Console.WriteLine(FetchSecretValueFromKeyVault(token));
} static string GetToken()
{
//169.254.169.254是Azure Instance Metadata service endpoint
WebRequest request = WebRequest.Create("http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.cn");
request.Headers.Add("Metadata", "true");
WebResponse response = request.GetResponse();
return ParseWebResponse(response, "access_token");
} static string FetchSecretValueFromKeyVault(string token)
{
//这里需要修改两个部分:
//你的KeyVault名称
//你的SecretName WebRequest kvRequest = WebRequest.Create("https://你的KeyVault名称.vault.azure.cn/secrets/你的SecretName?api-version=2016-10-01");
kvRequest.Headers.Add("Authorization", "Bearer " + token);
WebResponse kvResponse = kvRequest.GetResponse();
return ParseWebResponse(kvResponse, "value");
} private static string ParseWebResponse(WebResponse response, string tokenName)
{
string token = String.Empty;
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
String responseString = reader.ReadToEnd(); JObject joResponse = JObject.Parse(responseString);
JValue ojObject = (JValue)joResponse[tokenName];
token = ojObject.Value.ToString();
}
return token;
}
}
}

  12.运行完毕后,我们就可以在Azure VM里面显示Key Vault里面Secret的值

  

Azure Key Vault (3) 在Azure Windows VM里使用Key Vaule的更多相关文章

  1. Azure Key Vault (2) 使用Azure Portal创建和查看Azure Key Vault

    <Windows Azure Platform 系列文章目录> 请注意: 文本仅简单介绍如何在Azure Portal创建和创建Key Vault,如果需要结合Application做二次 ...

  2. 使用 Azure PowerShell 模块创建和管理 Windows VM

    Azure 虚拟机提供完全可配置的灵活计算环境. 本教程介绍 Azure 虚拟机的基本部署项目,例如选择 VM 大小.选择 VM 映像和部署 VM. 你将学习如何执行以下操作: 创建并连接到 VM 选 ...

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

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

  4. 排查在 Azure 中新建 Windows VM 时遇到的部署问题

    尝试创建新的 Azure 虚拟机 (VM) 时,遇到的常见错误是预配失败或分配失败. 当由于准备步骤不当,或者在从门户捕获映像期间选择了错误的设置而导致 OS 映像无法加载时,将发生预配失败. 当群集 ...

  5. 【Azure 环境】Azure Key Vault (密钥保管库)中所保管的Keys, Secrets,Certificates是否可以实现数据粒度的权限控制呢?

    问题描述 Key Vault (密钥保管库) 能不能针对用户授权实现指定用户只能访问某个或某些特定的key? 如当前有两个用户(User1, User2),在Key Vault中有10个Key,Use ...

  6. Azure Key Vault(二)- 入门简介

    一,引言 在介绍 Azure Key Vault 之前,先简单介绍一下 HSM(硬件安全模块). -------------------- 我是分割线 -------------------- 1,什 ...

  7. 如何加密 Windows VM 上的虚拟磁盘

    为了增强虚拟机 (VM) 的安全性以及符合性,可以加密 Azure 中的虚拟磁盘. 磁盘是使用 Azure 密钥保管库中受保护的加密密钥加密的. 可以控制这些加密密钥,以及审核对它们的使用. 本文详细 ...

  8. 【应用服务 App Service】App Service证书导入,使用Key Vault中的证书

    问题描述 正常情况下,如果需要为应用服务安装SSL证书,可以在证书准备好的情况,通过门户上传即可,详细步骤可以参考微软官方文档(在 Azure 应用服务中添加 TLS/SSL 证书:https://d ...

  9. Azure Key Vault (1) 入门

    <Windows Azure Platform 系列文章目录> 为什么要使用Azure Key Vault? 我们假设在微软云Azure上有1个场景,在Windows VM里面有1个.NE ...

随机推荐

  1. sql_视图和函数

    创建视图: create view xxx as select * from userinfo; 删除视图: drop view xxx 修改视图: alter view xxx as selete ...

  2. MRP routing设置释疑

    Jeffer9@gmail.com         工艺是指在不同工作中心执行的作业序列         作业的详细信息 Number of cycles 在该工作中心操作几个循环 Number of ...

  3. Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. python(35)- 异常处理

    一 错误和异常 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正)   #语法错误示范一 if #语法错误示范二 ...

  5. C语言关键字—-sizeof 、typedef、const、static、register、extern、#define

    关键字:sizeof .#define.typedef.const.static.register.extern sizeof 1. 作用:求数据所占得内存空间大小 2. 本质:求数据得类型所占的内存 ...

  6. linux 信号屏蔽

    <span style="font-size:18px;">#include <sys/types.h> #include <unistd.h> ...

  7. vs2012停止调试长时间不响应问题解决方法

    在vs2012命令提示符下,运行devenv.exe /resetuserdata又一次设置下环境解决.

  8. android中的常见对话框

    在android中对话框是一种常见的操作,常见的对话框有下面几种: 以下是xml布局文件: <LinearLayout xmlns:android="http://schemas.an ...

  9. GoogleFusionTablesAPI初探地图与云计算

    http://developer.51cto.com/art/200906/129324.htm http://yexiaochai.iteye.com/blog/1893735 http://yex ...

  10. 运维基础-IO 管道

    什么是文件描述符FD或者文件句柄? 通过构建一个带有编号标记的通道(文件描述符)的进程结构来管理打开的文件.今晨连接到文件,从而达到这些文件所代表的的数据内容或者设备.通过使用通道0.1.2(称为标准 ...