config.json
{
"Colors": {
"Favorite": "blue"
},
"DisableSSL": false,
"ConnectionStrings": {...
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
}
}
Startup.cs
services.AddMvc(opt => { if (env.IsProduction() && config["DisableSSL"] != "true")
{ opt.Filters.Add(new RequireHttpsAttribute()); } })
.AddJsonOptions(opt => opt.SerializerSettings
.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
当我发布到文件夹并在文件夹中打开CLI并set DisableSSl=true
并从CLI运行应用程序,然后浏览到localhost:5000
我被重定向到https://localhost。< / p>
为什么来自CLI的DisableSSL
不会覆盖配置文件中的那个?
如果我注释掉.Add(new RequireHttpsAttribute());
并重新发布并从CLI运行它不会重定向,我可以导航到localhost:5000的网站,以便发布工作正常。
我知道如何创建在SSL下运行的证书,这不是我的问题。
如何识别CLI会话中设置的环境变量?
(如果在Environment Variable
System Properties
下创建DisableSSL
,其值为true
。它也会被忽略,我会被重定向。)
答案 0 :(得分:1)
所以我进行了测试,也许我找到了你的问题:
采取以下appsettings
:
{
"Test1": true,
"Test2": "true",
}
现在让我们看看我们得到了什么:
var one = Configuration["Test1"];
//result: one == "True"
var two = Configuration["Test2"];
//result: two == "true"
因此,当你将环境变量设置为true时,我假设它将其提升为&#34; True&#34;由于区分大小写"True" != "true"
。尝试将其转换为小写。