1

I switched to SimpleMembershipProvider in an ASP.NET MVC 4 app. Everything else is working great but there's this problem...

When I try this code:

var password = Membership.GeneratePassword(Membership.MinRequiredPasswordLength, 0);

MinRequiredPasswordLength is always 0. The settings defined in the membership provider's Web.config section are not being read.

Here's the Membership default provider Web.config section:

<membership defaultProvider="AspNetSqlMembershipProvider">
    <providers>
         <clear />
             <add name="AspNetSqlMembershipProvider"
                  type="System.Web.Security.SqlMembershipProvider"
                  connectionStringName="DefaultConnection"
                  enablePasswordRetrieval="false"
                  enablePasswordReset="true"
                  requiresQuestionAndAnswer="false"
                  requiresUniqueEmail="false"
                  maxInvalidPasswordAttempts="5"
                  minRequiredPasswordLength="6"
                  minRequiredNonalphanumericCharacters="0"
                  passwordAttemptWindow="10"
                  applicationName="/Acad" />
    </providers>
</membership>

Here's the debug info I get:

enter image description here

Any hints?

Is the SimpleMembershipProvider so simple that it doesn't even use the Web.config section?

Solution for the moment

To overcome the SimpleMembershipProvider current limitation, I used this code to grab the value defined in Web.config:

MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
var defaultProvider = membershipSection.DefaultProvider;
ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
var minRequiredPasswordLength = int.Parse(providerSettings.Parameters["minRequiredPasswordLength"]);
Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480

3 Answers3

3

I switched to SimpleMembershipProvider in an ASP.NET MVC 4 app.

Erm, from the relevant section you have shown in your web.config this doesn't seem to be the case at all. You are sill using the AspNetSqlMembershipProvider.

If you want to switch to the SimpleMembershipProvider make sure that you have properly configured it:

<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider"
         type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"
    />
  </providers>
</membership>

This being said the SimpleMembershipProvider doesn't support setting those properties via web.config. It doesn't even use it. If you want such functionality you will have to implement it yourself.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I tried changing the `type`before seeing your answer. This is the error I get: `Provider must implement the class System.Web.Security.MembershipProvider'.` – Leniel Maccaferri Oct 03 '12 at 06:48
  • My bad... I copied/pasted from here http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ the wrong piece of code. I had this in the Web.config section: `WebMatrix.WebData.SimpleRoleProvider` instead of `WebMatrix.WebData.SimpleMembershipProvider`. – Leniel Maccaferri Oct 03 '12 at 06:50
  • But you should not longer use the `Membership` type. Use the `WebSecurity` class to access the SMP members. And as I stated in my answer the functionality you are looking for is not implemented by the SMP. If you want things like minRequiredPasswordLength you will have to implement it yourself. Don't forget that SMP is a simplified version of the Membership provider. That's why it's called Simple. – Darin Dimitrov Oct 03 '12 at 06:50
  • Yes... it's really simple! :) This is the first impediment I see... I switched to it because I can integrate it easily with my current Users table. Nonetheless, `minRequiredPasswordLength` is still 0. Looks like I'll have to add an app setting instead. – Leniel Maccaferri Oct 03 '12 at 06:53
  • Yes, you will have to manage this yourself. Here's a nice tutorial about the SMP: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/ – Darin Dimitrov Oct 03 '12 at 06:56
3

You can inherit from SimpleMembershipProvider and override MinRequiredPasswordLength:

public class YoursSimpleMembershipProvider : SimpleMembershipProvider
{
    private const int MIN_REQUIRED_PASSWORD_LENGTH = 6;

    private const int MIN_REQUIRED_NON_ALPHANUMERIC_CHARACTERS = 0;

    public override int MinRequiredPasswordLength
    {
        get
        {
            return MIN_REQUIRED_PASSWORD_LENGTH;
        }
    }
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

I found if I put minRequiredPasswordLength="3" in web.config,like this:

<membership defaultProvider="SimpleMembershipProvider" >
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider" 
         type="WebMatrix.WebData.SimpleMembershipProvider,WebMatrix.WebData"
         minRequiredPasswordLength="3"
    />
  </providers>     
</membership>

That no effect. I find the secret in MODEL.If you create an Internet application template,in AccountModels.cs there is a "RegisterModel",just like this:

    public class RegisterModel
    {
        [Required]
        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "密码")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "确认密码")]
        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
        public string ConfirmPassword { get; set; }
    }

In the Password property has a StringLength->MinimumLength . I think that is!

Some features removed in SimpleMembershipProvider,See here:http://sharifhkhan.com/programming/features-removed-in-simplemembershipprovider/

Hope this help you.

Andy
  • 94
  • 10