寻找灵活的方法来指定 ASP.NET 成员资格使用的连接字符串

发布于 2024-08-25 14:58:43 字数 1245 浏览 9 评论 0原文

我开发了一个 ASP.NET Web 应用程序,并使用 ASP.NET 会员资格提供程序。应用程序使用主应用程序数据库中的成员资格模式和所有必需的对象 然而,在开发过程中,我必须切换到各种数据库,以适应不同的开发和测试场景。为此,我有一个外部连接字符串部分文件和一个外部 appsettings 部分,它允许我不更改主 web.config,而是通过仅更改 appsettings 部分中的设置来轻松切换数据库。

我的文件如下:

<connectionStrings configSource="connections.config">
</connectionStrings>

<appSettings file="local.config">
    ....

ConnectionStrings 看起来像往常一样:

<connectionStrings>
  <add name="MyDB1" connectionString="..." ... />
  <add name="MyDB2" connectionString="..." ... />
  ....
</connectionStrings>

和 local.config 如下

<appSettings>
    <add key="ConnectionString" value="MyDB2" />

我的代码考虑使用此连接字符串

但 web.config 中的成员资格设置将连接字符串名称直接包含到设置中,因此

<add name="MembershipProvider" connectionStringName="MyDB2" ...>
....
<add name="RoleProvider" connectionStringName="MyDB2" ...>

,每个我也必须编辑它们才能使用新的数据库。

有什么方法可以配置会员资格提供程序以使用应用程序设置来选择会员资格数据库的数据库连接?或者“重定向”它以从其他地方读取连接设置?或者至少将其包含在某些外部文件(如 local.config)中,

也许是将 asp.net 会员资格提供程序包装到我自己的提供程序中的一些简单方法,该提供程序将仅从我想要的位置读取连接字符串并将其传递给原始会员资格提供程序,然后将整个会员功能委托给 ASP.NET 会员提供程序。

I develop an asp.net web application and I use ASP.NET membership provider. The application uses the membership schema and all required objects inside main application database
However, during development I have to switch to various databases, for different development and testing scenarios. For this I have an external connection strings section file and also an external appsettings section, which allow me to not change main web.config but switch the db easily, by changing setting only in appsettings section.

My files are as below:

<connectionStrings configSource="connections.config">
</connectionStrings>

<appSettings file="local.config">
    ....

ConnectionStrings looks as usual:

<connectionStrings>
  <add name="MyDB1" connectionString="..." ... />
  <add name="MyDB2" connectionString="..." ... />
  ....
</connectionStrings>

And local.config as below

<appSettings>
    <add key="ConnectionString" value="MyDB2" />

My code takes into account to use this connection string

But membership settings in web.config contains the connection string name directly into the setting, like

<add name="MembershipProvider" connectionStringName="MyDB2" ...>
....
<add name="RoleProvider" connectionStringName="MyDB2" ...>

Because of this, every time I have to edit them too to use the new db.

Is there any way to config membership provider to use an appsetting to select db connection for membership db? Or to "redirect" it to read connection setting from somewhere else? Or at least to have this in some external file (like local.config)

Maybe is some easy way to wrap asp.net membership provider intio my own provider which will just read connection string from where I want and pass it to original membership provider, and then just delegate the whole membership functionality to asp.net membership provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捎一片雪花 2024-09-01 14:58:43

我发现这个解决方案可以创建继承类并重写 Initialize() 方法来更改连接字符串。对我来说效果很好,但 web.config 设置可能有点棘手。我最终不得不创建 SqlMembershipProvider 和 SqlRoleProvider。

http://social. msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/260d8536-c39f-41ec-b181-4d452cf054b3

I found this solution to create inherited classes and override the Initialize() method to change the connection string. Worked well for me but the web.config settings can be a little tricky. I ended up having to create both a SqlMembershipProvider and a SqlRoleProvider.

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/260d8536-c39f-41ec-b181-4d452cf054b3

雨的味道风的声音 2024-09-01 14:58:43

在这种情况下,我对连接字符串使用不同的外部配置文件。可能(取决于您的具体情况)您可以使用 Visual Studio 预构建事件将文件复制到正确的位置。

I use different external configuration files for connection strings in this situation. Possibly (depending on your concrete situation) you can use Visual Studios pre build events to copy the files to the right places.

梦太阳 2024-09-01 14:58:43

事实上,经过一番思考,我找到了解决问题的方法。

我最初的想法是在 local.config 中设置特定的连接字符串名称,该名称将指向连接字符串部分中的连接字符串。但通过这种方法,我还必须在与成员资格相关的设置中更改连接字符串名称

新方法帮助我对连接字符串(由我的 DAL 和成员资格使用)进行单一引用,如下所示:

  1. in web.config 我有一个由 DAL 使用的设置,它存储连接字符串的名称:

  2. 与成员资格相关的设置引用相同的连接字符串


对于我的应用程序的所有实例,Web.config 都是相同的,因此不需要进行任何更改

  1. 我将连接串提取到外部文件中,并在 web.config 中使用此设置

    ;

  2. 然后我在文件connections.config中定义特定实例的特定连接字符串

    <连接字符串>
    <添加名称=“MyDB”connectionString=“数据源=...”providerName=“System.Data.SqlClient”/>

使用这种方法,我能够拥有一个特定于应用程序、对所有实例通用的、不可更改的 web.config,然后是特定实例的特定连接字符串,允许 DAL 和成员身份使用相同的数据库

Actually, after some more thinking, I found a solution for my problem.

My initial thought was to set specific connection string name in local.config, which will point to a connection string in conenction string sections. But with this approach I had to change connection string name also in membership-related settings

The new approach which helped me to have a single reference to connection string (to be used by botrh my DAL and membership) was the following:

  1. in web.config I have a setting which is used by my DAL, which store the name of the connection string:

    <add key="ConnectionString" value="ManagDB" />

  2. membership-related settings have reference to the same connection string

    <add name="MembershipProvider" connectionStringName="ManagDB" ...>
    <add name="RoleProvider" connectionStringName="ManagDB" ...>

Web.config will be tyhe same for all instances of my appliucation, so no change is needed in it

  1. I extract the connections strungs into an external file, with this setting in web.config

    <connectionStrings configSource="connections.config">
    </connectionStrings>

  2. Then I define the specific connection string for a certain instance in file connections.config

    <connectionStrings>
    <add name="MyDB" connectionString="Data Source=..." providerName="System.Data.SqlClient" />
    </connectionStrings>

Using this approach I was able to have a single, unchangeable web.config specific to the application, common to all instances, and then specific connection string to a certain instance which allow both DAL and membership to use the same database

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文