Avatar of realcoding
realcoding
Flag for United States of America asked on

two environments linked by a sql DB because of ROLES PROVIDER

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???

 
Web FrameworksASP.NET.NET Programming

Avatar of undefined
Last Comment
rdogmartin

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
rdogmartin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
realcoding

ASKER
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

rdogmartin

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23