access sub elements of membership providerhttp://forums.asp.net/t/1774335.aspx/1?access+sub+elements+of+membership+providerWed, 29 Feb 2012 08:43:42 -050017743354853273http://forums.asp.net/p/1774335/4853273.aspx/1?access+sub+elements+of+membership+provideraccess sub elements of membership provider <p>Greetings!!</p> <p>I want to access sub element of membership element in web.config through programming. Kindly tell me how I can access and change the settings programmatically. e.g. in the code below I want to access&nbsp;maxInvalidPasswordAttempts, &nbsp;minRequiredPasswordLength etc. and update through a web form. Please help.</p> <p>&lt;membership&gt;<br> &lt;providers&gt;<br> &lt;clear /&gt;<br> &lt;add name=AspNetSqlMembershipProvider<br> type=System.Web.Security.SqlMembershipProvider, System.Web,<br> Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a<br> connectionStringName=LocalSqlServer<br> enablePasswordRetrieval=false<br> enablePasswordReset=true<br> requiresQuestionAndAnswer=false<br> applicationName=/<br> requiresUniqueEmail=false<br> passwordFormat=Hashed<br> maxInvalidPasswordAttempts=5<br> minRequiredPasswordLength=6<br> minRequiredNonalphanumericCharacters=1<br> passwordAttemptWindow=10<br> passwordStrengthRegularExpression=<br> /&gt;<br> &lt;/providers&gt;<br> &lt;/membership&gt;</p> <p>&nbsp;Thanks</p> 2012-02-27T15:20:28-05:004853782http://forums.asp.net/p/1774335/4853782.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>Here's some sample code to get started. It's not for the exact section you wanted, but it will get you started on how to use the config API.</p> <pre class="prettyprint">protected void _button_Click(object sender, EventArgs e) { Configuration configFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(&quot;~&quot;); System.Web.Configuration.AuthenticationSection section = (System.Web.Configuration.AuthenticationSection) configFile.GetSection(&quot;system.web/authentication&quot;); section.Mode = System.Web.Configuration.AuthenticationMode.None; configFile.Save(); }</pre> 2012-02-28T01:00:09-05:004854227http://forums.asp.net/p/1774335/4854227.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>is there any way to update membership details programmatically?</p> <p>Thanks</p> 2012-02-28T07:09:47-05:004854339http://forums.asp.net/p/1774335/4854339.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>Define &quot;membership details&quot;. That is, what is it you want to update? If it's the configuration, then the method show by Brock works and allows updates. Brea in mind though, that changes to web.config will recycle the application, thus losing state.</p> 2012-02-28T08:09:46-05:004855278http://forums.asp.net/p/1774335/4855278.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Dave Sussman</h4> <p></p> <p>Define &quot;membership details&quot;. That is, what is it you want to update? If it's the configuration, then the method show by Brock works and allows updates. Brea in mind though, that changes to web.config will recycle the application, thus losing state.</p> <p></p> </blockquote> <p></p> <p>Hi,</p> <p>I have mentionad everything in my question. I want to update&nbsp;minRequiredPasswordLength,&nbsp;minRequiredNonalphanumericCharacters and such other details found in membership element in web.config file programmatically. So kindly tell me what is the way to update those details programmatically and safely.</p> <p>Many thanks</p> 2012-02-28T16:37:02-05:004855303http://forums.asp.net/p/1774335/4855303.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>Right. I wasn't sure whether you meant the physical web.config file, or the values as loaded at runtime. Try</p> <p></p> <pre style="font-family:DejaVu Sans Mono; font-size:13px; color:black; background:none repeat scroll 0% 0% white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af">Configuration</span>&nbsp;configFile&nbsp;=&nbsp;System.Web.Configuration.<span style="color:#2b91af">WebConfigurationManager</span>.OpenWebConfiguration(<span style="color:#a31515">&quot;~&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Web.Configuration.<span style="color:#2b91af">MembershipSection</span>&nbsp;section&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(System.Web.Configuration.<span style="color:#2b91af">MembershipSection</span>)configFile.GetSection(<span style="color:#a31515">&quot;system.web/membership&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af">ProviderSettings</span>&nbsp;settings&nbsp;=&nbsp;section.Providers[<span style="color:#a31515">&quot;AspNetSqlMembershipProvider&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings.Parameters[<span style="color:#a31515">&quot;minRequiredPasswordLength&quot;</span>]&nbsp;=&nbsp;<span style="color:#a31515">&quot;3&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings.Parameters[<span style="color:#a31515">&quot;minRequiredNonalphanumericCharacters&quot;</span>]&nbsp;=&nbsp;<span style="color:#a31515">&quot;3&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;configFile.Save();</pre> 2012-02-28T16:55:24-05:004855349http://forums.asp.net/p/1774335/4855349.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Dave Sussman</h4> <p></p> <p>Right. I wasn't sure whether you meant the physical web.config file, or the values as loaded at runtime. Try</p> <p></p> <pre style="font-family:DejaVu Sans Mono; font-size:13px; color:black; background:none repeat scroll 0% 0% white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af">Configuration</span>&nbsp;configFile&nbsp;=&nbsp;System.Web.Configuration.<span style="color:#2b91af">WebConfigurationManager</span>.OpenWebConfiguration(<span style="color:#a31515">&quot;~&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.Web.Configuration.<span style="color:#2b91af">MembershipSection</span>&nbsp;section&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(System.Web.Configuration.<span style="color:#2b91af">MembershipSection</span>)configFile.GetSection(<span style="color:#a31515">&quot;system.web/membership&quot;</span>); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#2b91af">ProviderSettings</span>&nbsp;settings&nbsp;=&nbsp;section.Providers[<span style="color:#a31515">&quot;AspNetSqlMembershipProvider&quot;</span>]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings.Parameters[<span style="color:#a31515">&quot;minRequiredPasswordLength&quot;</span>]&nbsp;=&nbsp;<span style="color:#a31515">&quot;3&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings.Parameters[<span style="color:#a31515">&quot;minRequiredNonalphanumericCharacters&quot;</span>]&nbsp;=&nbsp;<span style="color:#a31515">&quot;3&quot;</span>; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;configFile.Save();</pre> <p></p> </blockquote> <p></p> <p>Hi I tried almost this code (not exact) and I fall down into errors today in noon. And then I had to restore the config file again.</p> <p>So I want to know is it the safest way to update web.config file or try something else.</p> <p>Thanks</p> 2012-02-28T17:20:52-05:004855365http://forums.asp.net/p/1774335/4855365.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>What errors are you getting? The code works fine for me.</p> <p>This is really the only way to update the config file.</p> 2012-02-28T17:28:58-05:004855417http://forums.asp.net/p/1774335/4855417.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>Hi,</p> <p>I do not remember the error, but I will not repeat that again</p> <p>I will try your code and update here</p> <p>is it ok to use this code</p> <p>settings.Parameters.Set(&quot;maxInvalidPasswordAttempts&quot;, &quot;5&quot;);</p> <p></p> <p>Thanks</p> 2012-02-28T18:06:11-05:004856225http://forums.asp.net/p/1774335/4856225.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>Hi, I used that same code you provided and I am not getting any error, but the config file is not being saved at all.</p> <p>aspx page.</p> <p>&lt;table <br> cellpadding=&quot;4&quot; class=&quot;style1&quot;&gt;<br> &lt;tr&gt;<br> &lt;td class=&quot;style2&quot;&gt;<br> Enable Password Retrieval&lt;/td&gt;<br> &lt;td class=&quot;style3&quot;&gt;<br> &lt;asp:CheckBox ID=&quot;chkPassRet&quot; runat=&quot;server&quot;&gt;&lt;/asp:CheckBox&gt;<br> &lt;/td&gt;<br> &lt;td&gt;<br> &amp;nbsp;&lt;/td&gt;<br> &lt;/tr&gt;<br> &lt;tr&gt;<br> &lt;td class=&quot;style2&quot;&gt;<br> Enable Password Reset&lt;/td&gt;<br> &lt;td class=&quot;style3&quot;&gt;<br> &lt;asp:CheckBox ID=&quot;chkPassRes&quot; runat=&quot;server&quot;&gt;&lt;/asp:CheckBox&gt;<br> &lt;/td&gt;<br> &lt;td&gt;<br> &amp;nbsp;&lt;/td&gt;<br> &lt;/tr&gt;</p> <p>&lt;tr&gt;<br> &lt;td class=&quot;style2&quot;&gt;<br> &lt;asp:Button ID=&quot;btnSave&quot; runat=&quot;server&quot; Text=&quot;Save Changes&quot; <br> onclick=&quot;btnSave_Click&quot; /&gt;<br> &lt;/td&gt;<br> &lt;td class=&quot;style3&quot;&gt;<br> &amp;nbsp;&lt;/td&gt;<br> &lt;td&gt;<br> &amp;nbsp;&lt;/td&gt;<br> &lt;/tr&gt;<br> &lt;/table&gt;</p> <p></p> <p>Code behind:</p> <p>protected void btnSave_Click(object sender, EventArgs e)<br> {<br> <br> config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);<br> section = (MembershipSection)config.GetSection(&quot;system.web/membership&quot;);</p> <p>ProviderSettings providerSettings = section.Providers[&quot;AspNetSqlMembershipProvider&quot;];<br> providerSettings.Parameters[&quot;enablePasswordRetrieval&quot;] = chkPassRet.Checked.ToString();<br> providerSettings.Parameters[&quot;enablePasswordReset&quot;] = chkPassRes.Checked.ToString();<br> config.Save();</p> <p>}</p> <p>Kindly tell me, what is the reason...</p> <p>thnaks</p> 2012-02-29T07:26:55-05:004856318http://forums.asp.net/p/1774335/4856318.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>It could be a casing issue. Make sure that the attribute name in the code matches exactly with the one in web.config.</p> 2012-02-29T07:58:04-05:004856335http://forums.asp.net/p/1774335/4856335.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Dave Sussman</h4> <p></p> <p>It could be a casing issue. Make sure that the attribute name in the code matches exactly with the one in web.config.</p> <p></p> </blockquote> <p></p> <p>yes it was, and when you were answering , in the mean time I was updating my previous post.</p> <p>Kindly take a look at my previous post..</p> <p></p> 2012-02-29T08:05:00-05:004856379http://forums.asp.net/p/1774335/4856379.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>I'm not sure why it's not saving. It could be a permissions issue as you need write permissions on both the folder and file. Those permissions will depend upon the version of ASP.NET and IIS and how they are configured. For IIS6 you'll be running under the [NETWORK SERVICE] account, so that's the account that needs write permissions; for IIS7 (Windows 7 &amp; Server 2008) it'll depend upon the application pool: see http://learn.iis.net/page.aspx/624/application-pool-identities/.</p> 2012-02-29T08:24:22-05:004856409http://forums.asp.net/p/1774335/4856409.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>Hi, if it is permission issue, then why it is not giving any error at runtime while executing code?</p> <p></p> 2012-02-29T08:37:55-05:004856433http://forums.asp.net/p/1774335/4856433.aspx/1?Re+access+sub+elements+of+membership+providerRe: access sub elements of membership provider <p>I don't know why it's not giving an error. I'm just trying to give reasons why it might not be saving.</p> <p></p> 2012-02-29T08:43:42-05:00