怎样输入code提供密码的连接字符串中的ADO.Net实体数据模型 [英] How to in-code supply the password to a connection string in an ADO.Net Entity Data Model

查看:725
本文介绍了怎样输入code提供密码的连接字符串中的ADO.Net实体数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注这个教程就如何创建一个OData的服务。

I've been following this tutorial on how to create an OData service.

<一个href="http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx">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?"

如果我选择:不,从连接字符串中排除敏感数据,我将它设置在我的应用程序code

If I choose: "No, exclude sensitive data from the connection string. I will set it in my application code."

我不,在我的应用程序code插入密码看到我可以。 (我公司将它们存储在注册表加密)

I do not see where I can, "in my application code" insert the password. (My company stores them encrypted in the registry)

另外,我有我需要连接到,根据环境(开发,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
}

这篇关于怎样输入code提供密码的连接字符串中的ADO.Net实体数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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