【发布时间】:2021-08-11 15:58:23
【问题描述】:
在 ASP .NET Core Web 应用程序中,我注意到 连接字符串 可以放入 apsettings.json 或 secret.json文件如下:
appsettings.json
{
"ConnectionStrings": {
"DBConnectionString": "DefaultEndpointsProtocol=https;AccountName=name;AccountKey=the_key;EndpointSuffix=core.windows.net"
}
}
secret.json
{
"ConnectionStrings:DBConnectionString": "DefaultEndpointsProtocol=https;AccountName=name;AccountKey=the_key;EndpointSuffix=core.windows.net"
}
而且,它在 startup.cs 中使用。
public class startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDBContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DBConnectionString")));
}
}
问题:
以下是在 ASP .NET Core Web 应用程序中的一个控制器中传递 连接字符串 的方式:
BlogUploadController.cs
AccountName = "MyAccount";
AccountKey = "DGKC5745dfdG_+dkfkld";
string UserConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName=name;AccountKey=DGAKSECCDI654D_FGd;EndpointSuffix=core.windows.net", AccountName, AccountKey);
您可以看到我已经将“连接字符串”和“AccountKey”直接放在代码中(这很容易受到安全威胁)。任何人都可以像我在上面的例子中放入数据库连接字符串一样放入 apsettings.json 或 Secret.json。
提前致谢。
【问题讨论】:
-
谢谢您的回复先生。但我找不到办法。请你帮助我好吗。很抱歉打扰你。如果您至少可以分享一个链接,我将不胜感激。
-
另外,请阅读this。
-
感谢@RodrigoRodrigues 的支持。我会阅读你分享的文章。
标签: c# asp.net-core asp.net-core-mvc app-secret