简体   繁体   English

PROD中的asp.net秘密

[英]asp.net secrets in PROD

I have been using the new .net 4.7.1 functionality to manage secrets locally in DEV but now I want to publish my web app and of course that doesn't work in PROD. 我一直在使用新的.net 4.7.1功能来在DEV中本地管理机密,但现在我想发布自己的Web应用程序,但是在PROD中不起作用。

I am using a service principal to access Azure key vault in my app and was storing the App Id and Password in the local secrets file. 我正在使用服务主体访问我的应用程序中的Azure密钥库,并将应用程序ID和密码存储在本地机密文件中。 I can of course store all of my secrets in Key vault but I still need to supply it the service principal details to connect to that in the first place. 我当然可以将我的所有秘密存储在Key Vault中,但是我仍然需要向其提供服务主体详细信息,以便首先与之连接。

What's the best option here? 这里最好的选择是什么?

You need to store the values you use to open the key vault in the App settings of your application. 您需要在应用程序的“应用程序”设置中存储用于打开密钥库的值。 If your production environment is Azure, you can access the App settings of your application and set them there. 如果您的生产环境是Azure,则可以访问应用程序的“应用程序”设置并在此处进行设置。

Read this article to get an idea of the full picture. 阅读文章以获得完整的图片的想法。 But the jist is that you can use application secrets in a Azure key valut and then use app settings to store the credentials to access that key vault later on. 但最主要的是,您可以在Azure密钥值中使用应用程序密钥,然后使用应用程序设置存储凭据以供以后访问该密钥库。

You can use Managed Identities so you don't have to store any secret in your app: 您可以使用托管身份,因此不必在应用程序中存储任何秘密:

How to use managed identities for App Service and Azure Functions 如何将托管身份用于App Service和Azure函数

Once configured, add a reference to these nuget packages: Microsoft.Azure.Services.AppAuthentication and Microsoft.Azure.KeyVault 配置完成后,添加对这些nuget包的引用: Microsoft.Azure.Services.AppAuthenticationMicrosoft.Azure.KeyVault

In you application, you can access key vault secrets like that: 在您的应用程序中,您可以访问以下密钥库机密:

using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;

...

var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(
     new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync(
    "https://{{my-vault-name}}.vault.azure.net/", "{{my-secret}}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM