ASP.Net 用户名到电子邮件 [英] ASP.Net UserName to Email

查看:33
本文介绍了ASP.Net 用户名到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的 ASP.NET Identity (RTM),我想知道如何将注册和登录从用户名更改为电子邮件.

I am working with the new ASP.NET Identity (RTM) and I was wondering how would I go on about changing registering and login from being a UserName to an Email.

这个想法是我希望我的用户使用他们的电子邮件和密码进行注册(也可以使用外部登录获取电子邮件),然后他们在顶部设置一个显示名称/用户名.

The idea is that I want my users to sign up using their e-mail and a password (e-mail can also be acquired using external login) and they set-up a display name/username on top.

我查看了 IdentityUser,我可以看到 UserName 在那里,但是因为它被打包在无法更改的 ASP.Net Identity 中.

I've looked at IdentityUser and I can see that UserName is there, however since that is packed in ASP.Net Identity that can not be changed.

我知道我可以使用UserName"作为电子邮件,使用自定义验证器,然后为 ApplicationUser 提供一个名为 DisplayName 的额外属性,但这与其说是解决方案,不如说是一种黑客攻击.

I know I could use 'UserName' as a e-mail, with a custom validator and then have an extra attribute for ApplicationUser called DisplayName but that is more of a hack than a solution.

我希望我的问题很清楚.提前致谢.

I hope my question is clear. Thanks in advance.

推荐答案

如果您真的想使用电子邮件地址登录,那么恕我直言,您建议的hack"是最好的方法.因为,如果你坚持正确地做",你至少必须

If you really want to use e-mail address to log in, then, IMHO, the "hack" you suggested is the best approach. Because, if you insist on "doing it properly" you'll have to at least

  • 修改数据库架构,显然
  • 自己确保用户名的唯一性(您可以让数据库约束/索引为您执行此操作,但您必须找到处理错误并将其报告给用户的好方法)
  • 找到一个很好的替代品,而不是在代码中编写User.Identity.UserName"
  • 可能更多

另一方面,如果您决定破解"用户名字段,您需要

On the other hand, if you decide to "hack" the UserName field you need to

  • 修改 RegisterViewModel 验证(将 [EmailAddress] 添加到 UserName 属性),可能稍微自定义 [Display(Name=...)] 等.
  • 确保您的 AccountController 中使用的 UserManager.UserValidator 实例允许在电子邮件地址中使用特殊字符.为此,请确保其非默认构造函数如下所示:

  • modify RegisterViewModel validation (add [EmailAddress] to the UserName property), probably slightly customize [Display(Name=...)] etc.
  • make sure UserManager.UserValidator instance used in your AccountController allows special characters used in e-mail addresses. To do this, make sure its nondefault constructor looks like this:

public AccountController(UserManager<ApplicationUser> userManager)
{
    UserManager = userManager;
    var userValidator = UserManager.UserValidator as UserValidator<ApplicationUser>;
    userValidator.AllowOnlyAlphanumericUserNames = false;
}

我希望这可以帮助您权衡两种方法的利弊,并提出最佳解决方案.祝你好运!

I hope this could help you weigh the pros and cons of both approaches and come up with the best solution. Good luck!

这篇关于ASP.Net 用户名到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆