Azure WebJobs 是否支持用户机密?
Does Azure WebJobs support user secrets?
我有一个 ASP.NET Core 2.0 应用程序,我可以很好地使用用户机密。但是,我无法让它在 Azure WebJobs 应用程序或基本 windows 控制台应用程序(两者都是 .NET 4.6.1)中工作。我已经为 Microsoft.Extensions.Configuration.AddSecrets 添加了 nuget 并且方法 AddUserSecrets 在代码中编译得很好。
但是,我无法将用户机密添加到项目中。与 ASP.Net Core 2.0 应用程序一样,项目的右键单击上下文菜单中没有管理用户机密选项。因此,我尝试从命令行使用 dotnet user-secrets(我已在 ASP.NET Core 2.0 应用程序中成功使用)。它不起作用,因为我收到此错误。
No executable found matching command "dotnet-user-secrets"
从阅读其他帖子看来我需要 Microsoft.Extensions.SecretManager.Tools。我无法通过 nuget 添加它,因为我收到此错误:
所以我添加了:Severity Code Description Project File Line Suppression State
错误包 'Microsoft.Extensions.SecretManager.Tools 2.0.0' 的包类型 'DotnetCliTool' 不受项目 'xxx' 支持。 0
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
</ItemGroup>
到 csproj 并尝试恢复包。此时我收到另一个错误:
C:\Program Files\dotnet\sdk.0.0\NuGet.targets(102,5): error : Value cannot be null.\r [C:\...\xxx.csproj]
C:\Program Files\dotnet\sdk.0.0\NuGet.targets(102,5): error : Parameter name: key [C:\...\xxx]
所以现在我想知道 .NET 4.6.1 项目是否支持用户机密。如果是,我错过了什么?
编辑:我从 ASP.NET Core 2.0 应用程序中找到了 secrets.json 并将其复制到适合此控制台应用程序的新创建的文件夹中,并且正确获取了用户密码。因此,从使用的角度来看,它似乎可以正常工作。我遇到的问题实际上是用一种方便的方式设置秘密。
根据Microsoft.Extensions.SecretManager.Tools依赖,可以发现该工具目前只支持net core,不支持.net 4.6。
此外,根据这个article,你可以发现这个工具只在开发环境中使用。
The most important point is you should never store passwords or other sensitive data in source code, and you shouldn't use production secrets in development and test mode.
如果你想在azure webjobs中使用它,我建议你可以考虑使用Azure Key Vault。
The Secret Manager tool is used only in development. You can safeguard Azure test and production secrets with the Microsoft Azure Key Vault configuration provider. See Azure Key Vault configuration provider for more information.
此外,如果您还想使用Microsoft.Extensions.SecretManager,我们可以使用.net core 来创建azure webjobs。关于如何创建,可以参考这个。
我有一个 ASP.NET Core 2.0 应用程序,我可以很好地使用用户机密。但是,我无法让它在 Azure WebJobs 应用程序或基本 windows 控制台应用程序(两者都是 .NET 4.6.1)中工作。我已经为 Microsoft.Extensions.Configuration.AddSecrets 添加了 nuget 并且方法 AddUserSecrets 在代码中编译得很好。
但是,我无法将用户机密添加到项目中。与 ASP.Net Core 2.0 应用程序一样,项目的右键单击上下文菜单中没有管理用户机密选项。因此,我尝试从命令行使用 dotnet user-secrets(我已在 ASP.NET Core 2.0 应用程序中成功使用)。它不起作用,因为我收到此错误。
No executable found matching command "dotnet-user-secrets"
从阅读其他帖子看来我需要 Microsoft.Extensions.SecretManager.Tools。我无法通过 nuget 添加它,因为我收到此错误:
所以我添加了:Severity Code Description Project File Line Suppression State 错误包 'Microsoft.Extensions.SecretManager.Tools 2.0.0' 的包类型 'DotnetCliTool' 不受项目 'xxx' 支持。 0
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
</ItemGroup>
到 csproj 并尝试恢复包。此时我收到另一个错误:
C:\Program Files\dotnet\sdk.0.0\NuGet.targets(102,5): error : Value cannot be null.\r [C:\...\xxx.csproj]
C:\Program Files\dotnet\sdk.0.0\NuGet.targets(102,5): error : Parameter name: key [C:\...\xxx]
所以现在我想知道 .NET 4.6.1 项目是否支持用户机密。如果是,我错过了什么?
编辑:我从 ASP.NET Core 2.0 应用程序中找到了 secrets.json 并将其复制到适合此控制台应用程序的新创建的文件夹中,并且正确获取了用户密码。因此,从使用的角度来看,它似乎可以正常工作。我遇到的问题实际上是用一种方便的方式设置秘密。
根据Microsoft.Extensions.SecretManager.Tools依赖,可以发现该工具目前只支持net core,不支持.net 4.6。
此外,根据这个article,你可以发现这个工具只在开发环境中使用。
The most important point is you should never store passwords or other sensitive data in source code, and you shouldn't use production secrets in development and test mode.
如果你想在azure webjobs中使用它,我建议你可以考虑使用Azure Key Vault。
The Secret Manager tool is used only in development. You can safeguard Azure test and production secrets with the Microsoft Azure Key Vault configuration provider. See Azure Key Vault configuration provider for more information.
此外,如果您还想使用Microsoft.Extensions.SecretManager,我们可以使用.net core 来创建azure webjobs。关于如何创建,可以参考这个