Solved

two environments linked by a sql DB because of ROLES PROVIDER

Posted on 2010-11-16
3
357 Views
Last Modified: 2013-11-08
i have the following web.config and everything was fine until i added role provider.

i have two connection strings defined and code in my asp.net application to detect what machine is running the web app and pull the correct DB connection string for the environment (dev/ prod)

now i added role provider and everything is working but the definition in the web.config links the roles provider to a single DB in its declaration.

below is the section of my web.config in question.

how can i make it such that the roles in both DEV and PROD are independent and isolated as they should be???

 
0
Comment
Question by:realcoding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
rdogmartin earned 500 total points
ID: 34151898
Create a new role provider that inherits the existing one and override the Initialize() method to set the connection string to the desired value. For example, add this class to your project:

namespace MyNamespace
{
      public class MyRoleProvider : SqlRoleProvider
      {
            public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
            {
                  base.Initialize(name, config);

                  // Set to desired connection string name
                  string connectionStringName = "ISConnectionString_test";

                  string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

                  // Set private property of Membership provider.
                  FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                  connectionStringField.SetValue(this, connectionString);
            }
      }
}

Then replace your existing role definition in web.config with this:

<roleManager defaultProvider="MyRoleProvider">
      <providers>
            <clear />
            <add name="MyRoleProvider"
                        type="MyNamespace.MyRoleProvider"
                        connectionStringName="name of a real but dummy connection string from the connectionStrings section that will never actually get used. Its value could just be 'x'"
                        ... remaining properties...
            />
      </providers>
</roleManager>
0
 

Author Comment

by:realcoding
ID: 34155755
i added the following and it seems not to work. i added a break point to the class to check if it was pulling the correct connectionString and the break point does not get hit. what did i miss?

i try to detect the port and if it is not port 80 then use the dev connectionString, if it is port 80 then use the prod version


 
namespace STAR
{
    public class STARRoleProvider : SqlRoleProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);

            string connectionStringName = HttpContext.Current.Request.Url.Port == 80 ? "ISConnectionString_prod" : "ISConnectionString_test";
            // Set to desired connection string name
            
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

            // Set private property of Membership provider.
            FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
            connectionStringField.SetValue(this, connectionString);
        }
    }
}



<roleManager defaultProvider="STARRoleProvider">
      <providers>
        <clear />
        <add name="STARRoleProvider" type="STAR.STARRoleProvider" connectionStringName="x" applicationName="PMIS"/>
      </providers>
</roleManager>

Open in new window

0
 
LVL 6

Expert Comment

by:rdogmartin
ID: 34158120
Sorry, I forgot to include the enabled setting. In web.config, the first line should look like this:

<roleManager enabled="true" defaultProvider="STARRoleProvider">

I copied your code into one of my projects and confirmed it worked after making this change.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question