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

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

问题描述

我对新的ASP.NET身份(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,我可以看到该用户名是存在的,但是因为这是装在ASP.Net身份不能改变的。

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.

我知道我可以使用用户名作为电子邮件,使用自定义的验证,然后有一个额外的属性,ApplicationUser称为显示名称但更是一个黑客不是一个解决方案。

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.

推荐答案

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

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

  • 可能更加

在另一方面,如果你决定要黑客则需要用户名字段

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


  • 修改RegisterViewModel验证(添加[EmailAddress的]将UserName属性),可能稍微定制[显示(名称= ...)]等。

  • 使您的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天全站免登陆