1

I want to restrict password strength with below mentioned properties:: Password strength:

  • 1.Contain characters from three of the following four categories:

English uppercase characters (A through Z)
English lowercase characters (a through z)
Base 10 digits (0 through 9)
Non-alphabetic characters (for example, !, $, #, %)

  • 2.password Not contain the user's account name


I can easily achieve 1st condition by providing regex to PasswordStrengthRegularExpression attribute in web.config file under membership section
But the problem is with 2nd one(password Not contain the user's account name) what should i do to achieve that.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
RollerCosta
  • 5,020
  • 9
  • 53
  • 71
  • possible duplicate of [ASP .Net Membership (Where control goes on creating new user from config. window?)](http://stackoverflow.com/questions/8798892/asp-net-membership-where-control-goes-on-creating-new-user-from-config-window) – Eranga Jan 10 '12 at 06:09

1 Answers1

0

Check the page I link to in the following post.

Basically, you write your own Dataannotation, so you should be able to modify it to see if the password contains the username instead of being equal to the username.

Opposite of [compare(" ")] data annotation in .net?

I just realised you posted that question also.

So you should change the classname in the blogpost to something like "CanNotContainAttribute" and then also change to the same name for the constructor.

In the IsValid method, you should check if one string contains the other, i.e. if Password contains Username, where Username is here refered to as the OtherProperty if the annotation is applied to the password property in the model file.

And then you apply the annotation to the Password property by doing:

[CanNotContain("UserName")]
public string Password { get; set; }

The original blog post I refer to is: http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2

Community
  • 1
  • 1
Sverker84
  • 465
  • 3
  • 8
  • and where should i use that validation . i.e on which property ?? – RollerCosta Jan 10 '12 at 06:09
  • you code i'll work if one not using ASP .NET Membership/But here i am using it . so doing this results nothing. – RollerCosta Jan 10 '12 at 06:17
  • Ok, perhaps someone else can give you a good answer then. My thought is that perhaps you can create a custom membership provider and implement the functionality you want there. Have a look at: [link]http://www.codeproject.com/KB/aspnet/CustomMembershipProviders.aspx[/link] or google "Custom membership provider asp.net" – Sverker84 Jan 10 '12 at 06:27