ASP.NET MVC 成员身份 - 使用了错误的用户名大小写

发布于 2024-09-11 11:50:56 字数 534 浏览 9 评论 0原文

当用户在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相守太难 2024-09-18 11:50:56

用户名在整个提供程序堆栈中不区分大小写,并且主体设置为用于身份验证的任何值。

如果您需要强制区分大小写,则需要按照您的描述保护所有身份验证点或实现自定义主体/身份。

我强烈推荐第一个,如果你选择第二个,我会为你祈祷。 ;-)

祝你好运。

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.

凉栀 2024-09-18 11:50:56

你总是可以执行你自己的案件。就像如果这些是人们的真实姓名(即使是一些奇怪的名字)你可以这样做:

 TextInfo culture = new CultureInfo("en-US", false).TextInfo;
  FormsService.SignIn(culture.ToTitleCase(model.UserName), model.RememberMe);

并使其成为标题大小写。当然,这不是 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文