Can't you just override the UserName property and add your annotation?
Can I change the displayed name of the UserName field in ASP.NET MVC IdentityUser?
-
08-07-2023 - |
Pergunta
I'm using ASP.NET Identity, and have extended the IdentityUser class to add my own data.
I'm using data annotations to set the display name, etc. of some fields, but because the UserName field is baked-in, I can't annotate that. Since I'm planning to store an email address in that field, that's one that I really do want to rename!
If data annotations are a non-starter (and I assume they are), is there any other way to achieve the same result?
Solução
Outras dicas
The way I'm handling it is by simply not exposing UserName
. I have a separate Email
property, and I then set UserName
to the same thing when saving, behind the scenes.
Also, if you intend on using email addresses as user names, you'll need to have the following:
UserManager.UserValidator = new UserValidator<User>(UserManager) { AllowOnlyAlphanumericUserNames = false };
Otherwise, it won't allow you to set the user name to a valid email address.