Asp.Net User Membership Options from web.config

I am currently writing a custom membership provider for asp.net, and the problem I am facing is that I don’t know how to provide the options for the custom membership provider in the same way as you give the standard asp.NET Memberships in the file web.config such as password length.

+6
source share
3 answers

When you retrieve your own class from the MembershipProvider , you must override the Initialize() method, it has the following signature:

 public override void Initialize(string name, NameValueCollection config); 

System.Collections.NameValueCollection is a dictionary where you will find the parameters recorded in the web.config . These parameters are specified in the same way as you specify parameters for "standard" providers (as attributes). Each dictionary entry has an attribute name key and, as a value, an attribute value (like string ).

 public class MyMembershipProvider : MembershipProvider { public override void Initialize(string name, NameValueCollection config) { base.Initialize(name, config); _enablePasswordReset = config.GetBoolean("enablePasswordReset", true); } } 

Where, in my example, GetBoolean() is an extension method declared somewhere as follows:

 public static bool GetBoolean(this NameValueCollection config, string valueName, bool? defaultValue) { object obj = config[valueName]; if (obj == null) { if (!defaultValue.HasValue) throw new WarningException("Required field has not been specified."); return defaultValue.Value; } bool value = defaultValue; if (obj is Boolean) return (bool)obj; IConvertible convertible = obj as IConvertible; try { return convertible.ToBoolean(CultureInfo.InvariantCulture); } catch (Exception) { if (!defaultValue.HasValue) throw new WarningException("Required field has invalid format."); return defaultValue.Value; } } 
+5
source

If your provider receives from MembershipProvider : ProviderBase , then all boot files must be downloaded and applied to the web.config form.

Consider creating custom IPrincipal and / or IIdentity - this is sometimes the best extension point, and since not everyone knows about it, it is often not used.

+2
source

In the same way, you define standard .net membership:

 <membership defaultProvider="MyCustomMembershipProvider" userIsOnlineTimeWindow="30"> <providers> <clear /> <add name="MyCustomMembershipProvider" type="Namespace.MyCustomMembershipProvider" connectionStringName="db_ConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Encrypted" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" /> <add name="StandardMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="db_ConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Encrypted" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" /> </providers> </membership> 
+1
source

All Articles