问题描述
我一直在关注如何创建 OData 服务的本教程.
I've been following this tutorial on how to create an OData service.
http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx
而且它完美无瑕......但是,在实体数据模型向导中,当它要求您选择您的数据连接"时,它会给您这个警告.
And it works flawlessly ... but, in the Entity Data Model Wizard, when it asks you to "Choose Your Data Connection" it gives you this warning.
此连接字符串似乎包含连接到数据库所需的敏感数据(例如密码).将敏感数据存储在连接字符串中可能会带来安全风险.是否要将此敏感数据包含在连接字符串?"
"This connection string appears to contain sensitive data (for example, a password) that is required to connect to the database. Storing sensitive data in the connection string can be a security risk. Do you want to include this sensitive data in the connection string?"
如果我选择:否,从连接字符串中排除敏感数据.我将在我的应用程序代码中设置它."
If I choose: "No, exclude sensitive data from the connection string. I will set it in my application code."
我看不到在哪里可以在我的应用程序代码中"插入密码.(我的公司将它们加密存储在注册表中)
I do not see where I can, "in my application code" insert the password. (My company stores them encrypted in the registry)
另外,我需要连接多个数据库,具体取决于环境(Dev、CA 或 Prod),我需要知道连接字符串中引用了哪些数据库才能获得正确的密码.
Plus, I have multiple DBs that I need to connect to, depending on the environment (Dev, CA, or Prod) and I need to know what DB is referenced in the connection string to get the correct password.
谢谢.
推荐答案
创建上下文时,可以设置连接字符串.要构建此连接字符串,您可以使用 EntityConnectionStringBuilder
解析没有密码的连接字符串,然后使用其他 ConnectionStringBuilder
解析内部连接字符串,具体取决于您的浏览器.然后你可以设置密码并传递给构造函数.
When you create your context, you can set a connection string. To build this connection string, you can parse the connection string without the password with an EntityConnectionStringBuilder
and then parse the inner connection string with an other ConnectionStringBuilder
, depending on your browser. Then you can set the password and pass it to the constructor.
var originalConnectionString = ConfigurationManager.ConnectionStrings["your_connection_string"].ConnectionString;
var entityBuilder = new EntityConnectionStringBuilder(originalConnectionString);
var factory = DbProviderFactories.GetFactory(entityBuilder.Provider);
var providerBuilder = factory.CreateConnectionStringBuilder();
providerBuilder.ConnectionString = entityBuilder.ProviderConnectionString;
providerBuilder.Add("Password", "Password123");
entityBuilder.ProviderConnectionString = providerBuilder.ToString();
using (var context = new YourContext(entityBuilder.ToString()))
{
// TODO
}
这篇关于如何在代码中为 ADO.Net 实体数据模型中的连接字符串提供密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!