在 ASP .NET Core Web 应用程序中,我注意到可以将连接字符串放入apsettings.json或secret.json文件中,如下所示:
应用设置.json
{
"ConnectionStrings": {
"DBConnectionString": "DefaultEndpointsProtocol=https;AccountName=name;AccountKey=the_key;EndpointSuffix=core.windows.net"
}
}
秘密.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 应用程序中的一个控制器中传递的方式:
博客上传控制器.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。
提前致谢。