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 maxInvalidPasswordAttempts, minRequiredPasswordLength
etc. and update through a web form. Please help.
Define "membership details". 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.
Define "membership details". 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.
Hi,
I have mentionad everything in my question. I want to update minRequiredPasswordLength, 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.
sophia_asp
Member
521 Points
469 Posts
access sub elements of membership provider
Feb 27, 2012 03:20 PM|LINK
Greetings!!
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 maxInvalidPasswordAttempts, minRequiredPasswordLength etc. and update through a web form. Please help.
<membership>
<providers>
<clear />
<add name=”AspNetSqlMembershipProvider”
type=”System.Web.Security.SqlMembershipProvider, System.Web,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
connectionStringName=”LocalSqlServer”
enablePasswordRetrieval=”false”
enablePasswordReset=”true”
requiresQuestionAndAnswer=”false”
applicationName=”/”
requiresUniqueEmail=”false”
passwordFormat=”Hashed”
maxInvalidPasswordAttempts=”5”
minRequiredPasswordLength=”6”
minRequiredNonalphanumericCharacters=”1”
passwordAttemptWindow=”10”
passwordStrengthRegularExpression=”“
/>
</providers>
</membership>
Thanks
BrockAllen
All-Star
27432 Points
4890 Posts
MVP
Re: access sub elements of membership provider
Feb 28, 2012 01:00 AM|LINK
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.
protected void _button_Click(object sender, EventArgs e) { Configuration configFile = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); System.Web.Configuration.AuthenticationSection section = (System.Web.Configuration.AuthenticationSection) configFile.GetSection("system.web/authentication"); section.Mode = System.Web.Configuration.AuthenticationMode.None; configFile.Save(); }DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
sophia_asp
Member
521 Points
469 Posts
Re: access sub elements of membership provider
Feb 28, 2012 07:09 AM|LINK
is there any way to update membership details programmatically?
Thanks
Dave Sussman
All-Star
37716 Points
5005 Posts
ASPInsiders
MVP
Re: access sub elements of membership provider
Feb 28, 2012 08:09 AM|LINK
Define "membership details". 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.
sophia_asp
Member
521 Points
469 Posts
Re: access sub elements of membership provider
Feb 28, 2012 04:37 PM|LINK
Hi,
I have mentionad everything in my question. I want to update minRequiredPasswordLength, 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.
Many thanks
Dave Sussman
All-Star
37716 Points
5005 Posts
ASPInsiders
MVP
Re: access sub elements of membership provider
Feb 28, 2012 04:55 PM|LINK
Right. I wasn't sure whether you meant the physical web.config file, or the values as loaded at runtime. Try
sophia_asp
Member
521 Points
469 Posts
Re: access sub elements of membership provider
Feb 28, 2012 05:20 PM|LINK
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.
So I want to know is it the safest way to update web.config file or try something else.
Thanks
Dave Sussman
All-Star
37716 Points
5005 Posts
ASPInsiders
MVP
Re: access sub elements of membership provider
Feb 28, 2012 05:28 PM|LINK
What errors are you getting? The code works fine for me.
This is really the only way to update the config file.
sophia_asp
Member
521 Points
469 Posts
Re: access sub elements of membership provider
Feb 28, 2012 06:06 PM|LINK
Hi,
I do not remember the error, but I will not repeat that again
I will try your code and update here
is it ok to use this code
settings.Parameters.Set("maxInvalidPasswordAttempts", "5");
Thanks
sophia_asp
Member
521 Points
469 Posts
Re: access sub elements of membership provider
Feb 29, 2012 07:26 AM|LINK
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.
aspx page.
<table
cellpadding="4" class="style1">
<tr>
<td class="style2">
Enable Password Retrieval</td>
<td class="style3">
<asp:CheckBox ID="chkPassRet" runat="server"></asp:CheckBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
Enable Password Reset</td>
<td class="style3">
<asp:CheckBox ID="chkPassRes" runat="server"></asp:CheckBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
<asp:Button ID="btnSave" runat="server" Text="Save Changes"
onclick="btnSave_Click" />
</td>
<td class="style3">
</td>
<td>
</td>
</tr>
</table>
Code behind:
protected void btnSave_Click(object sender, EventArgs e)
{
config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
section = (MembershipSection)config.GetSection("system.web/membership");
ProviderSettings providerSettings = section.Providers["AspNetSqlMembershipProvider"];
providerSettings.Parameters["enablePasswordRetrieval"] = chkPassRet.Checked.ToString();
providerSettings.Parameters["enablePasswordReset"] = chkPassRes.Checked.ToString();
config.Save();
}
Kindly tell me, what is the reason...
thnaks