Search code examples
asp.net-mvcasp.net-membership

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 logging 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?


Solution

  • 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.