ASPNET身份2.0电子邮件和用户名重复 [英] AspNet Identity 2.0 Email and UserName duplication

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

问题描述

我目前的Asp.Net MVC 5项目任务的电子邮件地址的对的用户名的。现在我想的升级的ASPNET身份V1.0到的 2.0 的利用其所有新功能(<一个href=\"http://blogs.msdn.com/b/webdev/archive/2014/03/20/test-announcing-rtm-of-asp-net-identity-2-0-0.aspx\">see这里)。

My current Asp.Net MVC 5 project mandates Email address for UserName. Now I want to upgrade ASPNet Identity v1.0 to v2.0 to leverage all its new features (see here).

不过,ASPNET身份V2.0增加的电子邮件的作为一个单独的列到的用户的表格,并将相应的属性添加到的 IdentityUser 的类

However, ASPNet Identity v2.0 adds Email as a separate column to the Users table and adds a corresponding property to the IdentityUser class.

我不希望重复的用户名的到这个新的电子邮件的列。我怎么能地图的IdentityUser这个电子邮件物业使用现有的用户名和列放大器;属性?是否有可能的忽略的这个电子邮件财产和跳过用户表将列?有没有人试过这种?

I don't want to duplicate UserName into this new Email column. How can I map this Email Property of IdentityUser to use existing UserName column & property? Is it possible to ignore this Email property and skip adding the column in the Users table? Has anybody tried this?

请分享。

更新

这是身份的2.0局限性。我们不能忽视电子邮件财产或离开它空。有些身份的功能将无法正常工作。 (

This is the identity 2.0 limitation. We cannot ignore Email property or leave it Null. Some of the Identity functionality will not work. :(

推荐答案

您可以尝试下列操作之一:

You can try one of these:


  1. 尝试通过重写电子邮件属性在用户类,并取消映射,或使用忽略它流畅的API。

  1. Try to ignore it by either overriding Email property in your User class and unmapping it or using fluent API.

public class ApplicationUser : IdentityUser
{
  // ....

  [NotMapped]
  public override string Email { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ApplicationUser>().Ignore(u => u.Email);
}


  • 当您注册用户只需确保您填写电子邮件用户名

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            // ...
        }
    }
    


  • 当然,你总是可以忽略电子邮件列,如果你不打算使用它,因为它允许空值,它会只是坐在你的 AspNetUsers 表一堆空值,不是最好的方法,但记住,忽略它,你可能会失去新的功能,ASP.NET身份2可以提供你可能需要使用的。

    Of course, you can always ignore Email column if you're not going to use it, since it allows NULLs, it'll just be sitting in your AspNetUsers table with bunch of NULLs, not the best approach but remember that by ignoring it you might lose new features that ASP.NET Identity 2 might offer that you might want to use.

    注意但是我不知道如果选择1号将在电子邮件物业工作,因为它可能使用遍布新标识的地方code。值得一试,虽然。我知道这是你如何能摆脱其他列的,如果你不需要它们。我个人碰巧使用新的电子邮件属性/列,所以我还没有尝试过。

    NOTE However I'm not sure if option number 1 will work on Email property since it's probably used all over the place in new Identity code. Worth a try though. I know that's how you can get rid of other columns if you don't need them. I personally happen to use new Email property/column so I haven't tried it.

    不知道,如果它可以帮助你,但想到我会为了以防万一分享。

    Not sure if it helps you, but thought I'd share it just in case.

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

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