你能在运行时更改ConnectionString配置值吗?或者...我是否需要这样做?

7

首篇文章,我是一个完全不懂.Net/C#的新手,被扔到了深水区!

由于某人离职,只有我有空闲时间,所以我继承了一个C# Web应用程序。但我并没有 .Net、C# 的知识!

该应用程序由世界各地的人们使用。他们使用公司登录详细信息进行登录,因此根据他们所在的位置(欧洲、美国或印度),他们会登录到不同的服务器。

编写该应用程序的人无法弄清如何根据位置在 web.config 中切换 ConnectionString,因此为每个域名复制了整个应用程序!唯一的变化是每个复制版本的 web.config 中的单个 IP 地址!然后做了一个简单的 Web 前端页面,根据用户所在的世界位置将用户带到“他们”的应用程序版本!

我想做的第一件事是转移到单个版本以进行维护,因此我需要能够切换连接字符串或如何登录?

我花了几天时间试图找出如何从我的登录类获取 ConnectionString(在 web.config 中定义),只发现 web.config 中设置的值似乎是只读的,因此我不能更改它们。

那么我的第一个问题是,我是在错误的方向上努力吗?我能否只设置 AspNetActiveDirectoryMembershipProvider(稍后见代码)所需的所有信息,并从我的登录类调用它?或者连接字符串路线是在 .Net/C# 中设置连接的唯一方法吗?因此,我确实需要找出如何在运行时更改/指定/添加值。

我能想到三种可能性:-(第一个是我已经停滞不前的)

  1. 从我的登录类更改 ADService 的 ConnectionString?
  2. 更改 AspNetActiveDirectoryMembershipProvider 使用的内容,因此从我的登录类中神奇地让它使用 web.config 中定义的 EMEA_ADService 或 PACIFIC_ADService?
  3. 是否可以定义新的 connectionString 并从我的登录类调用 AspNetActiveDirectoryMembershipProvider,而完全不使用 web.config 进行此连接?

这是我的/他的 web.config 文件和我的 Login 类的一部分

Web.config 剪辑

<connectionStrings>
    <add name="ADService" connectionString="LDAP://12.345.67.8" />          *---- Original ConnectionString (IP address changed)----* 
    <add name="EMEA_ADService" connectionString="LDAP://12.345.67.8" />     *---- Added by me playing around unsuccessfully! ----* 
    <add name="PACIFIC_ADService" connectionString="LDAP://12.345.67.9" />  *---- Added by me playing around unsuccessfully! ----* 
    ~
  </connectionStrings>

<authentication mode="Forms">
      <forms loginUrl="~/Login.aspx" timeout="2880" />     *---- The background class for this popup (Login.aspx.cs) is where I'm currently trying to affect ConnectionString----* 
    </authentication>
                                                           *---- Pretty sure this is the bit that actually does the login verification----* 
    <membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
      <providers>
        <clear />
        <add name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider,            System.Web, Version=4.0.0.0, Culture=neutral,            PublicKeyToken=12345678" connectionStringName="ADService" applicationName="/." description="ADService" />
      </providers>
    </membership>

在发现似乎无法更改 ConnectionString 后,这就是我在课堂上的最新进展了!

Login.aspx.cs 的片段:

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings; //this is now working :)
        string userDomain = Environment.UserDomainName;  //Probably don't need this, it seems to give the login domain on the machine. Don't know yet if that will be the users machine or the server the app runs on?
        if (connections.Count != 0)
        {
            foreach (ConnectionStringSettings connection in connections)
            {
                string testname = connections["ADService"].Name;
                string testConnectionString = connections["ADService"].ConnectionString;
                connections["ADService"].ConnectionString = "LDAP://12.345.67.9";
                testConnectionString = connections["ADService"].ConnectionString;

非常期待您能提供任何提示!

顺便说一下,我在工作中已经请求了一门 .Net/C# 课程! ;)


非常抱歉。祝你好运。 - Mike G
你可以动态组装一个名称,因此如果你能确定你所在的位置,例如“太平洋”,那么你可以使用 Connections[location + '_ADService'] 来获取正确的连接字符串。 - HABO
2个回答

5
您不希望更改现有的连接字符串。相反,您需要更改数据访问层用于调用不同服务堆栈的连接字符串。然后,您可以根据任何输入参数在运行时选择连接字符串,这在您的情况下可能是一个IP范围。
这里有三个网址提供更多信息:asp.net mvc多个连接字符串在一个DataAccess层处理多个连接字符串,和http://msdn.microsoft.com/en-us/library/aa479086.aspx。特别是Microsoft的文章,因为它实际上从架构的角度来看待解决类似问题的正确模式。祝您好运!

谢谢你的帮助。我想答案是,我需要先学习一些关于.NET和C#的知识!所以这个更改将不得不回到待办事项列表中,直到我至少对如何在这个环境中实现我想做的事情有一个模糊的想法为止!哈哈 - user2008415
@user2008415 不客气!你一开始就面对了一些具有挑战性的问题,我希望你能找到所有你寻找的答案!祝你好运,并感谢你接受我的答案。 - David L

0

Web.config 不能在运行时修改。我建议通过网站上的登录链接或组合框设置某种标志,供人们选择他们想要登录的位置。服务器的工作不是弄清楚用户想做什么。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接