I am using the membership provider with ASP.NET. The maximum length of a UserName is 50. Is there a way to increase it?
I've already tried to increase the column size in the db but MemberShip.CreateUser fails for long usernames with a ProviderError
2 Answers
Answers 1
You can always override the UserName property, and specify the StringLength through DataAnnotations. By default, this would be in your IdentityModel.cs file.
Add this line to your ApplicationUser
class:
[StringLength(80)] public new string UserName {get;set;}
I just ran a test and created a User with a 70 character-length string.
Answers 2
Increase maximum UserName length in a custom ASP.Net Membership
Your custom membership provider should implement System.Web.Security.MembershipProvider
(see MembershipProvider). This has a method that you have to override:
public abstract MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);
This is where you must implement your own validation of the method parameters including the allowable length of the incoming parameters. You can see the built in code for System.Web.Security.SqlMembershipProvider
as an example which validates the username length to be no greater than 128 unicode characters.
So you will have to do 2 things.
- In your database schema increase the username table field length to the correct maximum allowable size.
- In your custom ASP.Net Membership provider (your class that inherits from
System.Web.Security.MembershipProvider
) alter the validation in theCreateUser
method so that you check on the correct maximum allowed number of characters.
If you are not sure what class of yours is implementing MembershipProvider
you can either search for code files for that string OR you can look in your web.config
file here to get the type.
Code snipit from SqlMembershipProvider
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { if (!SecUtility.ValidateParameter(ref password, true, true, false, 128)) // check on 128 maximum length { status = MembershipCreateStatus.InvalidPassword; return (MembershipUser) null; }
0 comments:
Post a Comment