Mr. Flibble
Mr. Flibble

Reputation: 27003

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?

Upvotes: 0

Views: 878

Answers (2)

chobo2
chobo2

Reputation: 85765

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

Upvotes: 1

Sky Sanders
Sky Sanders

Reputation: 37084

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.

Upvotes: 3

Related Questions