.Net Core是否支持每个环境的用户密码? [英] Does .Net Core support User Secrets per environment?

查看:35
本文介绍了.Net Core是否支持每个环境的用户密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 appsettings.Development.json 中指定的开发环境的连接字符串和 appsettings.Staging.json

在开发和暂存之间进行切换所需要做的就是导航到项目属性中的"Visual Studio调试"选项卡,并更改 ASPNETCORE_ENVIRONMENT 环境变量的值.

现在,出于安全原因,我当然不想在 appsettings.*.json 中使用连接字符串.所以我将其移至用户机密".

问题是-似乎只有一个 secrets.json 文件可供所有环境使用.没有 secrets.Development.json secrets.Staging.json .这意味着在我通过Visual Studio的调试"选项卡从开发"环境切换到暂存"环境之后,我还需要在 secrets.json 中手动更改连接字符串,这违背了对环境的内置支持的目的.

按环境不支持用户密钥是否正确?如果是这样-是否有另一种方法可以避免在切换环境时避免手动修改Secret连接字符串?

解决方案

如果使用 dotnet user-secrets --help 检查工具的参数,您会看到可以为每个配置指定不同的机密(调试,发布,以及您想要的其他任何版本),但不能按环境进行.考虑一下,这不是一个错误的决定.

ASPNETCORE_ENVIRONMENT 环境变量用于告诉您的应用程序当前计算机或容器是开发,生产还是其他环境,因此它可以选择适当的设置文件.不应将此环境变量从一个应用程序执行更改为下一个应用程序执行.即使使用容器,环境变量也会从主机传递到容器,并且在容器的生命周期内不会发生变化.

出于开发目的,应该将 secrets 文件放在每台计算机上,因此无需在每个环境中保留单独的文件.使用单独的文件进行配置更有意义,这使开发人员可以简单地从Dev更改为Release或Testing或他们可能具有的任何其他自定义配置.

Let's say I have connection string for Development environment specified in appsettings.Development.json and connection string for the Staging environment specified in appsettings.Staging.json

All I need to do to switch between Development and Staging is to navigate to Visual Studio Debug tab in project properties and change the value for ASPNETCORE_ENVIRONMENT environment variable.

Now, of course I don't want to have connection string in appsettings.*.json for security reasons. So I move it to User Secrets.

Problem is - it seems there is just one secrets.json file that is used by all the environments. There are no secrets.Development.json or secrets.Staging.json. This means after I switch from Development to Staging environment via Visual Studio Debug tab I then also need to change connection strings manually in secrets.json which kind of defeats the purpose of having built-in support for the environments.

Is this correct that User Secrets are not supported on per-environment basis? If so - is there another approach that would avoid having to modify Secret connection string manually when switching environments?

解决方案

If you check the tool's parameters with dotnet user-secrets --help you'll see you can specify different secrets per configuration (Debug, Release, any other you want) but not per environment. Which is not a bad decision if you think about it.

The ASPNETCORE_ENVIRONMENT environment variable is meant to tell your application whether the current machine or container is a Development, Production or other environment, so it can pick the appropriate settings file. This environment variable isn't expected to change from one application execution to the next. Even when using containers, the environment variables are passed from the host to the container and aren't expected to change during the container's lifetime.

The secrets files are supposed to be per machine, for development purposes, so there's no need to keep separate files per environment. It makes much more sense to use separate files for configuration, allowing developers to simply change from Dev to Release or Testing or any other custom configuration they may have.

这篇关于.Net Core是否支持每个环境的用户密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆