views:

42

answers:

2

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, if 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 any better way to do this?

+2  A: 

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.

Sky Sanders
praying about it seems a little extreme, it is nearly the weekend - dont forget to take it easy mate :)
Harry
No chance I'm taking the latter option! Thanks.
Mr. Flibble
+1  A: 

You could always just enforce your own case. Like If these are people 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 db at least).

chobo2
ToTitleCase, interesting - wasn't aware of that method.
Harry