ASP.NET MVC 成员身份 - 使用了错误的用户名大小写
当用户在 ASP.NET MVC 项目中使用成员资格服务的默认实现登录时,他们将使用登录时使用的用户名(无论大小写)登录。
例如,我创建一个名为 John 的用户。如果我随后以 joHN 身份登录,则 User.Identity.Name 将等于 joHN。我希望它等于 John,即实际用户的登录名。
目前我正在像这样解决这个问题:
var membershipUser = Membership.GetUser(model.UserName);
var membershipUserName = Membership.GetUserNameByEmail(membershipUser .Email);
FormsService.SignIn(membershipUserName, model.RememberMe);
而不是默认实现:
FormsService.SignIn(model.UserName, model.RememberMe);
它似乎有点迂回。有更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户名在整个提供程序堆栈中不区分大小写,并且主体设置为用于身份验证的任何值。
如果您需要强制区分大小写,则需要按照您的描述保护所有身份验证点或实现自定义主体/身份。
我强烈推荐第一个,如果你选择第二个,我会为你祈祷。 ;-)
祝你好运。
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.
你总是可以执行你自己的案件。就像如果这些是人们的真实姓名(即使是一些奇怪的名字)你可以这样做:
并使其成为标题大小写。当然,这不是 100% 您所要求的,但它可能比您当前的解决方案更好(因为这至少节省了对数据库的调用)。
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:
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).