SimpleMembershipProvider MinRequiredPasswordLength always returns 0 despite it being defined as 6 in Web.config membership provider section
Asked Answered
S

3

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"]);
Sage answered 3/10, 2012 at 5:59 Comment(0)
P
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.

Proletarian answered 3/10, 2012 at 6:44 Comment(5)
I tried changing the typebefore seeing your answer. This is the error I get: Provider must implement the class System.Web.Security.MembershipProvider'.Sage
My bad... I copied/pasted from here blog.longle.net/2012/09/25/… the wrong piece of code. I had this in the Web.config section: WebMatrix.WebData.SimpleRoleProvider instead of WebMatrix.WebData.SimpleMembershipProvider.Sage
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.Proletarian
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.Sage
Yes, you will have to manage this yourself. Here's a nice tutorial about the SMP: blog.longle.net/2012/09/25/…Proletarian
A
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;
        }
    }
}
Amling answered 5/12, 2012 at 10:12 Comment(1)
Thanks for the answer, but in my case I was interested in getting the value from the Web.config file.Sage
A
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.

Adequate answered 26/11, 2013 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.