ASP.NET MVC membership - wrong username casing used
When a user logs in with the default implementation of the membership service in an ASP.NET MVC project, they are logged in with a username with whatever case they used when loggi开发者_JS百科ng in.
For example, I create a user called John. If I then log in as joHN, then User.Identity.Name will equal joHN. I would like it to equal John, the actual user's login name.
At the moment I'm getting around this like so:
var membershipUser = Membership.GetUser(model.UserName);
var membershipUserName = Membership.GetUserNameByEmail(membershipUser .Email);
FormsService.SignIn(membershipUserName, model.RememberMe);
instead of the default implementation:
FormsService.SignIn(model.UserName, model.RememberMe);
It seems a bit circuitous. Is there a better way to do this?
Username is case-insensitive throughout the provider stack and the principal is set with whatever value is used to authenticate.
If you need to enforce case-sensitivity, you will need to either guard all points of authentication as you describe or implement a custom principal/identity.
I strongly recommend the first and will pray for you if you choose the second. ;-)
Good luck.
You could always just enforce your own case. Like if these are people's real names (even if some weird names) you could just do something like:
TextInfo culture = new CultureInfo("en-US", false).TextInfo;
FormsService.SignIn(culture.ToTitleCase(model.UserName), model.RememberMe);
And make it title case. Of course this is not 100% what you asked for, but it might be better than your current solution (as this saves a call to the database at least).
精彩评论