Reputation: 44
I set the value MinimunLenghtValidator modifying the ApplicationUserManager class
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager() : base(new UserStore<ApplicationUser>(new DataContext()))
{
PasswordValidator = new MinimumLengthValidator(8);
}
}
Now, I want to retrieve that value to use, for example, in the AccountViewModel validation (look at the entire StringLength line)
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = IWANTTHEVALUEHERE)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
Anybody could tell me how to get the value.
Regards
Upvotes: 1
Views: 676
Reputation: 42516
Attributes require a constant value, so you would need to hard code it in your StringLength
attribute. Alternatively, you can declare it as a public constant somewhere, probably on your ApplicationUserManager
class
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public const int PasswordLength = 8;
public ApplicationUserManager() : base(new UserStore<ApplicationUser>(new DataContext()))
{
PasswordValidator = new MinimumLengthValidator(ApplicationUserManager.PasswordLength);
}
}
Then, in your view model you can reference the same constant
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = ApplicationUserManager.PasswordLength)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
The UserManager<T>
class has a public PasswordValidator
property which you can use to get access to the validator. The issue is that this property is an IIdentityValidator<string>
so it can work with different types of validators. You need to cast this property to a MinimumLengthValidator
in order to access the properties of that validator. You would need code similar to this:
int requiredLength = 0;
ApplicationUserManager manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var validator = manager.PasswordValidator as MinimumLengthValidator;
if (validator != null)
{
requiredLength = validator.RequiredLength;
}
Upvotes: 2
Reputation: 3051
You can get the properties from the membership class.
As example:
// System.Web.Security.Membership
Membership.MinRequiredPasswordLength
You can also change the values in your web.config, this is mine:
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<!--<membership>
Upvotes: 0