Username length validation error
See original GitHub issueHi there
Firstly a great job with this framework. I am trying to change the UserName length to 256. I’ve read many posts where it’s suggested to override the UserName StringLength. But for some reason it doesn’t appear to work for me
Your Abp package version. 3.8.0 Your base framework: .Net Framework or .Net Core. .Net Core Exception message and stack trace if available. Your request is not valid! The following errors were detected during validation. - The field UserName must be a string with a maximum length of 32.
Steps needed to reproduce the problem. Download ASP.NET Core 2.0 Multipage version Run the web app
Changed the Username length to 256 in both the User and UserDto classes
public class User : AbpUser<User>
{
public const string DefaultPassword = "123qwe";
// OVERRIDE here
[StringLength(256)]
[MaxLength(256)]
public override string UserName { get; set; }
public static string CreateRandomPassword()
{
return Guid.NewGuid().ToString("N").Truncate(16);
}
public static User CreateTenantAdminUser(int tenantId, string emailAddress)
{
var user = new User
{
TenantId = tenantId,
UserName = AdminUserName,
Name = AdminUserName,
Surname = AdminUserName,
EmailAddress = emailAddress
};
user.SetNormalizedNames();
return user;
}
}
No matter what I try the validator still thinks I’m trying to use max length 32, am I overriding in the wrong error?
Issue Analytics
- State:
- Created 5 years ago
- Comments:36 (15 by maintainers)
Top GitHub Comments
Great - that worked! Now managed through the HandleEvent override and the error is gone. Records all created as expected.
For completeness I as you suggested I needed to override the Update handler as follows:
public override void HandleEvent(EntityUpdatedEventData<AbpUserBase> eventData) { using (_unitOfWorkManager.Current.SetTenantId(null)) { UserAccountOverride userAccount = (UserAccountOverride)_userAccountRepository.FirstOrDefault(ua => ua.TenantId == eventData.Entity.TenantId && ua.UserId == eventData.Entity.Id); if (userAccount != null) { userAccount.UserName = eventData.Entity.UserName; userAccount.EmailAddress = eventData.Entity.EmailAddress; userAccount.LastLoginTime = eventData.Entity.LastLoginTime; _userAccountRepository.Update(userAccount); } } }
Thanks very much for your help with this - I don’t think I’d have got there on my own, but I now have some new insight into ABP.
@techcolin yes @ebicoglu is talking about ASP.NET Core version but your’s is ASP.NET MVC 5.x. Workaround below might work for you;
modelBuilder.Entity<User>().Property(t => t.UserName).HasMaxLength(50);
do it like this;Add below lines into your User class;
Change length of UserName field in CreateUserDto class.
Create a new class for UserAccount synchronization;
Configuration.ReplaceService<UserAccountSynchronizer, MyUserAccountSynchronizer>();
If you have problems while updating a user, you can also override Update event;