Setting Membership/Profile/Role provider's connection string at runtime...?http://forums.asp.net/t/997608.aspx/1?Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Tue, 24 May 2011 07:41:34 -04009976081308551http://forums.asp.net/p/997608/1308551.aspx/1?Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Setting Membership/Profile/Role provider's connection string at runtime...? <p>How can I set the connection string for the built-in membership, profile and role providers at runtime?</p> <p>Since each site in our multi-site application uses a separate&nbsp;API to retrieve its specific connection string (via key, for security purposes), I cannot set the connection string to my providers in the web.config file. I need to be able to set the connection strings programmatically at runtime, but I cannot find how to implement this. </p> <p>I thought many people must have run into this problem, but I can't seem to find anything regarding this.Can anyone here help me with this problem? </p> <p>&nbsp;</p> <p>Sebastian</p> 2006-06-08T04:12:19-04:001308676http://forums.asp.net/p/997608/1308676.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>You'd better configure each web site as an application. After seoarating the application, you can set the connection string for each web site easily in web.config.</p> <p><font size="2"><font face="Arial">&lt;connectionStrings&gt;</font></font></p> <p class="MsoNormal" style="margin:0in 0in 0pt"><span style="font-size:10pt; color:black"><font face="Arial"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;remove name=LocalSqlServer/&gt;</font></span></p> <p class="MsoNormal" style="margin:0in 0in 0pt"><span style="font-size:10pt; color:black"><font face="Arial"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&lt;add name=&quot;LocalSqlServer&quot; connectionString=&quot;<span style="">Data Source=localhost;Initial Catalog=appservicesdb;Integrated Security=True&quot; providerName=&quot;System.Data.SqlClient</span>&quot;/&gt;<span style=""></span></font></span></p> <p class="MsoNormal" style="margin:0in 0in 0pt"><span style="font-size:10pt; color:black"><font face="Arial"><span style="">&nbsp;&nbsp;&nbsp; </span>&lt;/connectionStrings&gt;</font></span></p> 2006-06-08T08:15:04-04:001309139http://forums.asp.net/p/997608/1309139.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>No, that's not an option for us. We HAVE to set the connection s tring programmatically, we have no choice. I will explain:</p> <p>I work for an interactive/ marketing agency, and the project I am working on is a large site for a well-known multi-national company. This company has 200&#43; websites with a common user database and their own hosting. For their own security reasons, they do not give out database connection strings to be hard-coded. Instead, they developed an application (with an API given out to their interactive agencies) to which you pass a predetermined key as a parameter and get back the connection string. <strong>This is our only option.</strong></p> <p>So, based on this, you're telling me that there's no way in the world to set the connection string outside of the web.config file?!? That just can't be, there has to be a way.</p> <p>If it helps, I am using a custom membership provider to handle interfacing with our client's common user database.</p> 2006-06-08T16:28:46-04:001309148http://forums.asp.net/p/997608/1309148.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <blockquote><span class="icon-blockquote"></span> <h4>sebatwerk</h4> <p>I work for an interactive/ marketing agency, and the project I am working on is a large site for a well-known multi-national company. This company has 200&#43; websites with a common user database and their own hosting. For their own security reasons, they do not give out database connection strings to be hard-coded. Instead, they developed an application (with an API given out to their interactive agencies) to which you pass a predetermined key as a parameter and get back the connection string. <strong>This is our only option.</strong></p> <p></p> </blockquote> <p></p> <p>Keep in mind that using this system of connection string management also helps when you use&nbsp;a multi-server development/staging/production process. We do not have to change the connection strings in our application because we are always passing the same key to the ConnectionStringManager API, but getting back different strings depending on which server the calling application is running. It's really very handy.</p> 2006-06-08T16:34:22-04:001310881http://forums.asp.net/p/997608/1310881.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>zhuhua1006 ,</p> <p>So can you, or anyone else here, come up with a solution to help me out?</p> 2006-06-10T09:48:23-04:002113160http://forums.asp.net/p/997608/2113160.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>did you ever find a solution to this?</p> 2008-01-16T15:25:34-05:002209437http://forums.asp.net/p/997608/2209437.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>&nbsp;Sounds like your agency is working for the same multi national company that&nbsp; mine is. any one find anything on this??<br> </p> <p>&nbsp;</p> <p><br> --B <br> </p> 2008-03-03T21:57:46-05:002211924http://forums.asp.net/p/997608/2211924.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>&nbsp;OK figured it out...and my multi national company is now saved...ha ha</p> <p>This is a one liner gang........</p> <p>you will need to create your own providers however, We simply downloaded the SampleProviderToolkitSampleProviders from microsoft to lessen the blow.<br> </p> <p><a href="http://download.microsoft.com/download/a/b/3/ab3c284b-dc9a-473d-b7e3-33bacfcc8e98/ProviderToolkitSamples.msi" target="_blank">http://download.microsoft.com/download/a/b/3/ab3c284b-dc9a-473d-b7e3-33bacfcc8e98/ProviderToolkitSamples.msi</a> <br> </p> <p>Class to modify</p> <p>SQLConnectionHelper.cs</p> <p>Method to modify </p> <p>--------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br> &nbsp;</p> <p>internal static string GetConnectionString(string specifiedConnectionString, bool lookupConnectionString, bool appLevel)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> <br> //Your Conn String goes here!!</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Factory.ConnectionString;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> </p> <p><br> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------</p> <p>&nbsp;<br> we used our own namespace to be used in the web.congif as well.</p> <p>complile that bad boy and your golden..</p> <p>have fun<br> </p> <p>--b<br> &nbsp;&nbsp;&nbsp; <br> &nbsp;</p> <p>&nbsp;<br> &nbsp;</p> 2008-03-04T19:50:21-05:002577344http://forums.asp.net/p/997608/2577344.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>&nbsp;A simpler, albeit somewhat eyebrow-raising solution is just modifying the connection string in the providers early enough in the request's lifecycle:</p> <p>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void SetProviderConnectionString(string connectionString)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Set private property of Membership, Role and Profile providers. Do not try this at home!!<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var connectionStringField = Membership.Provider.GetType().GetField(&quot;_sqlConnectionString&quot;, BindingFlags.Instance | BindingFlags.NonPublic);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (connectionStringField != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionStringField.SetValue(Membership.Provider, connectionString);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var roleField = Roles.Provider.GetType().GetField(&quot;_sqlConnectionString&quot;, BindingFlags.Instance | BindingFlags.NonPublic);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (roleField != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; roleField.SetValue(Roles.Provider, connectionString);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var profileField = ProfileManager.Provider.GetType().GetField(&quot;_sqlConnectionString&quot;, BindingFlags.Instance | BindingFlags.NonPublic);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (profileField != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; profileField.SetValue(ProfileManager.Provider, connectionString);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;Calling this method from Global.asax.cs inside Application_PreRequestHandlerExecute does the job. Haven't tested it too much, but even if something doesn't work, it just means it needs to be done earlier. No guarantees this will work with future versions of the framework, although most likely it will.</p> <p>Gabor <br> </p> 2008-08-25T02:06:20-04:002660200http://forums.asp.net/p/997608/2660200.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>RGabo you're a star. Your post prompted me to create my own custom SqlMembershipProvider class to tidy this up.</p> <pre class="prettyprint">namespace MyNamespace { public class MyMembershipProvider : SqlMembershipProvider { public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { base.Initialize(name, config); // Update the private connection string field in the base class. string connectionString = &quot;my new connection string value that I get from a custom decryption class not shown here&quot;</pre><pre class="prettyprint"> <SPAN class=cmt>// Set private property of Membership provider.</SPAN> FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic); connectionStringField.SetValue(this, connectionString); } } }</pre> <P>&nbsp;Then in app/web.config I put this:</P> <P mce_keep="true">&nbsp;<pre class="prettyprint"> &lt;<SPAN class=tag>membership</SPAN><SPAN class=attr> defaultProvider=</SPAN><SPAN class=attrv>"MyMembershipProvider"</SPAN>&gt; &lt;<SPAN class=tag>providers</SPAN>&gt; &lt;<SPAN class=tag>clear</SPAN> /&gt; &lt;<SPAN class=tag>add</SPAN><SPAN class=attr> name=</SPAN><SPAN class=attrv>"MyMembershipProvider"</SPAN> <SPAN class=attr> type=</SPAN><SPAN class=attrv>"MyNamespace.MyMembershipProvider, MyAssembly"</SPAN> <SPAN class=attr> connectionStringName=</SPAN><SPAN class=attrv>"name of a real but dummy connection string from the connectionStrings section that will never actually get used. Its value could just be 'x'"</SPAN> <SPAN class=attr> applicationName=</SPAN><SPAN class=attrv>"MyApp"</SPAN> <SPAN class=attr> enablePasswordRetrieval=</SPAN><SPAN class=attrv>"false"</SPAN> <SPAN class=attr> enablePasswordReset=</SPAN><SPAN class=attrv>"true"</SPAN> <SPAN class=attr> requiresQuestionAndAnswer=</SPAN><SPAN class=attrv>"false"</SPAN> <SPAN class=attr> requiresUniqueEmail=</SPAN><SPAN class=attrv>"false"</SPAN> <SPAN class=attr> minRequiredNonalphanumericCharacters=</SPAN><SPAN class=attrv>"0"</SPAN> <SPAN class=attr> passwordFormat=</SPAN><SPAN class=attrv>"Hashed"</SPAN> /&gt; &lt;/<SPAN class=tag>providers</SPAN>&gt; &lt;/<SPAN class=tag>membership</SPAN>&gt;</pre>&nbsp; <p>&nbsp;</p> 2008-10-03T00:00:48-04:002825115http://forums.asp.net/p/997608/2825115.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>antmx, does this have to go into a separate project from the main one where it is used? I simply created a new class within my main project (LT) like so:</p> <p>==== In Class COBWEBMembershipProvider.vb<br> </p> <p>Imports System.Reflection<br> <br> Namespace COBWEB<br> &nbsp;&nbsp;&nbsp; Public Class COBWEBMembershipProvider<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inherits SqlMembershipProvider<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Me.Initialize(name, config)<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Update the private connection string field in the base class.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim connString As String = &quot;Data Source=Some Connection String Here&quot;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Set private membership provider property. Reflection is used to discover the attributes of a field and<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' provide access to the field metadata.<br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Note: Not sure about the parameter for GetType. C# example did not have it, but VB needs something.<br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim connStringField As FieldInfo = GetType(FieldInfo).BaseType.GetField(&quot;_sqlConnectionString&quot;, BindingFlags.Instance Or _<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BindingFlags.NonPublic)<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connStringField.SetValue(Me, connString)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub<br> <br> &nbsp;&nbsp;&nbsp; End Class<br> End Namespace <br> </p> <p>&nbsp;</p> <p>Add added this to my web.config <br> </p> <p>===== In Web.Config <br> </p> <p>&nbsp;&nbsp;&nbsp; &lt;membership defaultProvider=&quot;COBWEBMembershipProvider&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;providers&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;clear/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;add name=&quot;COBWEBMembershipProvider&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type=&quot;COBWEB.COBWEBMembershipProvider, LT&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionStringName=&quot;LTDummy&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; applicationName=&quot;LT&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enablePasswordRetrieval=&quot;false&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enablePasswordReset=&quot;true&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minRequiredPasswordLength=&quot;5&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requiresQuestionAndAnswer=&quot;true&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; requiresUniqueEmail=&quot;false&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; passwordFormat=&quot;Hashed&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxInvalidPasswordAttempts=&quot;5&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minRequiredNonalphanumericCharacters=&quot;0&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; passwordAttemptWindow=&quot;10&quot;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; passwordStrengthRegularExpression=&quot;&quot;/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/providers&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/membership&gt;<br> <br> </p> <p>But i get this error: <br> </p> <p><b>System Message:</b> Could not load type 'COBWEB.COBWEBMembershipProvider' from assembly 'LT'. (C:\Inetpub\wwwroot\LT\LT\LT\web.config line 33) </p> <p>&nbsp;I'm looling at the Toolkit Sample Providers now.<br> </p> 2008-12-22T16:45:55-05:002825173http://forums.asp.net/p/997608/2825173.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>Since the compiler is telling me it can't find the type (in the *dll), i am going to try putting the code in a separate project and copying to the bin folder.<br> </p> 2008-12-22T17:15:49-05:002849011http://forums.asp.net/p/997608/2849011.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>RGabo and Antmx - THANK YOU!&nbsp; That's exactly what I needed.&nbsp; Searched all over the internet and finally found this thread, I did exactly what you did Antmx and it works like a charm!</p> 2009-01-06T06:46:52-05:002861782http://forums.asp.net/p/997608/2861782.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>Gewgala, so you didn't have any issues? I still haven't had it work in the VB version I created so I think I'll try using the exact code in C#.<br> </p> 2009-01-12T09:49:51-05:002862586http://forums.asp.net/p/997608/2862586.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? Not a single issue, it just worked. One thing I did differently was I erased &quot;, MyAssembly&quot; in the &quot;type&quot; property as shown below, as I'm working on a regular website: &lt;add name=&quot;MyMembershipProvider&quot; type=&quot;MyNamespace.MyMembershipProvider&quot; ... /&gt; 2009-01-12T16:10:40-05:002955176http://forums.asp.net/p/997608/2955176.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>My application supports failover and we're using SQLEXPRESS so no DB capability exists for doing this.</p> <p>I'm using a custom Profile Provider which works to initially set the connection string at runtime but I now need the capability to modify the connection string a second time to update the failover machine.</p> <p>I've tried recalling the &quot;Initialize&quot; routine to get this to work and it runs and looks like the connection is changed but the provider's connection string is never changed.</p> <p>Any help is greatly appreciated.&nbsp;</p> 2009-02-20T15:07:06-05:002985564http://forums.asp.net/p/997608/2985564.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>Thanks, Gewgala. I know it's been a while since you responded, but I finally got back to this in my project. The C# code code works fine as you and others have stated. The code I converted to VB works for the Membership provider, but not the RoleProvider. I don't know why. Of course, it could be something with the translation. I'm not up on reflection yet.</p> <p>Next, up is finding a way to integrate this with our shops standard data access layer. I found a good article on another site that may prove very useful.<br> </p> 2009-03-05T17:34:48-05:003114426http://forums.asp.net/p/997608/3114426.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? I have the same issue. Membership, Role, Profile custom providers work fine on my local IIS. I deploy this to production and no joy. <font color="#0000ff" size="2"><font color="#0000ff" size="2"></font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">using</font></font><font size="2"><font color="#000000"> System.Web.Security;</font></font></font></font><font size="2"> <p></font>using<font size="2"> System.Reflection;</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> <p>public</font></font><font size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">class</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">MyRoleProvider</font></font><font size="2"> : </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">SqlRoleProvider...</font></font></p> <font size="2"></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">public</font></font><font size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">override</font></font><font size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">void</font></font><font size="2"> Initialize(</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">string</font></font><font size="2"> name, System.Collections.Specialized.</font><font color="#2b91af" size="2"><font color="#2b91af" size="2">NameValueCollection</font></font><font size="2"> config)</font><font size="2"> <p>{</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">base</font></font><font size="2">.Initialize(name, config);</font><font size="2"> <p></font><font color="#008000" size="2"><font color="#008000" size="2">// Update the private connection string field in the base class.</font></font></p> <p><font color="#008000" size="2"><font color="#008000" size="2">//Using a class that manages my connection strings. It will pass the correct one based on the server name the application is running on.</p> </font></font><font size="2"></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ServerConnect</font></font><font size="2"> sc = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ServerConnect</font></font><font size="2">();</font><font size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">string</font></font><font size="2"> connectionString = sc.GetConnectionString();</font><font size="2"> <p></font><font color="#008000" size="2"><font color="#008000" size="2">// Set private property of Role provider.</p> </font></font><font size="2"></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">FieldInfo</font></font><font size="2"> connectionStringField = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">typeof</font></font><font size="2">(</font><font color="#2b91af" size="2"><font color="#2b91af" size="2">SqlRoleProvider</font></font><font size="2">).GetField(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;_sqlConnectionString&quot;</font></font><font size="2">, </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">BindingFlags</font></font><font size="2">.Instance | </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">BindingFlags</font></font><font size="2">.NonPublic);</font><font size="2"> <p></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">if</font></font><font size="2"> (connectionStringField != </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">null</font></font><font size="2">)</p> connectionStringField.SetValue(</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">this</font></font><font size="2">, connectionString);</font><font size="2"> <p>}</p> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2"></font></font>&nbsp;</p> <p><font color="#2b91af">web.config</font></p> <font color="#2b91af"><font color="#0000ff" size="2"><font color="#0000ff" size="2">&lt;</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">profile</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">defaultProvider</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">MyProfileProvider</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">enabled</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">true</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&gt;</font></font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> <p>&lt;</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">providers</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&gt;</p> &lt;</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">clear</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">/&gt;</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> &lt;</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">add</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">name</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">MyProfileProvider</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">connectionStringName</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">conn</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">applicationName</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">CodersExpo</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">type</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">MyProfileProvider</font></font><font color="#000000" size="2">&quot;</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">/&gt;</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> <p>&lt;/</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">providers</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&gt;</p> </font></font> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2"></p> &lt;/</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">profile</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">&gt;</font></font> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2">Everything works great on my box. I created roles, profile, members ...</font></font></p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2">On the remote I get an error like this:</font></font></p> <font color="#0000ff" size="2"><font color="#0000ff" size="2"> <h2><i>Configuration Error</i> </h2> <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "><b>Description: </b>An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. <br> <br> <b>Parser Error Message: </b>System.Web.Security.SqlRoleProvider._sqlConnectionString<br> <br> <b>Source Error:</b> <br> <br> <table class="" width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td class=""><pre class="prettyprint"><pre>Line 54: &lt;providers&gt;</pre> Line 55: &lt;clear/&gt; Line 56: &lt;add name=&quot;MyRoleProvider&quot; connectionStringName=&quot;conn&quot; applicationName=&quot;CodersExpo&quot; type=&quot;MyRoleProvider&quot;/&gt; Line 57: &lt;/providers&gt; Line 58: &lt;/roleManager&gt; <pre></pre></pre></td> </tr> </tbody> </table> <br> <b><font face="Verdana">Source File: </font></b>d:\hosting\charcoaldev\web.config<b><font face="Verdana"> &nbsp;&nbsp; Line: </font></b>56 <br> <br> </font><font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> <hr width="100%" color="silver" size="1"> <p><b><font face="Verdana">Version Information:</font></b>&nbsp;Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433 </p> <p>&nbsp;I don't get it??? Please help. I know the remote connection string is correct because I can query the DB.</font></p> </font></font> 2009-04-23T21:30:27-04:003150845http://forums.asp.net/p/997608/3150845.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>Thanks. I got it to work. There must have been a problem in the C# to VB translation. Now, I'm trying to get Memebership access to work with a Linked Server. I haven't found a solution for this yet.<br> </p> 2009-05-08T14:24:46-04:003320794http://forums.asp.net/p/997608/3320794.aspx/1?Re+Setting+Membership+Profile+Role+provider+s+connection+string+at+runtime+Re: Setting Membership/Profile/Role provider's connection string at runtime...? <p>Hi Charcoal,</p> <p>Did you find a solution to your custom role provider / config error? I'm using the same technique and am getting the exact same error as you describe on deployment (works locally though). </p> <p>Thanks,</p> <p>Mark<br> </p> 2009-07-30T08:18:02-04:00