如何配置用户上下文/表?

时间:2013-07-01 08:11:07

标签: asp.net-mvc-5 asp.net-identity

发布新的ASP.NET MVC 5 Preview后,如何配置Users上下文/表?

在MVC 4中,我只使用自己的User类,然后将WebSecurity初始化为它,就这样:

WebSecurity.InitializeDatabaseConnection(connectionString, "System.Data.SqlClient", userTableName, userIdColumn, userNameColumn, autoCreateTables);

我希望向Users类添加其他属性 - 如何?

3 个答案:

答案 0 :(得分:2)

我认为,这可以解决您的问题:


Models \ IdentityModels.cs 中,您可以重新定义自己的用户模型:

public class ApplicationUser : IdentityUser
{
    /* identity field from database */
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    [Required]
    public bool Internal { get; set; }

    public string UserFullName { get; set; }

    public string UserEmail { get; set; }

    public ApplicationUser()
        : base()
    {
        Internal = false;
    }

    public ApplicationUser(string userName)
        : base(userName)
    {
        Internal = false;
    }

}

现在您可以使用 OnModelCreating()重写和 ToTable()方法更改默认AspNet表的映射:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Change the name of the table to be Users instead of AspNetUsers
        modelBuilder.Entity<IdentityUser>().ToTable("User");
        modelBuilder.Entity<ApplicationUser>().ToTable("User");

        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("User_Claim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("User_Login");
        modelBuilder.Entity<IdentityUserRole>().ToTable("User_Role");
    }
}

最后,您将在数据库中看到以下表格: 用户,角色,User_Role,User_Claim,User_Login ,而不是AspNetUsers,AspNetRoles,AspNetUsersRoles,AspNetUsersClaims,AspNetUserLogins。

当然,用户表格将包含其他字段: UserId (int identity),内部 UserFullName UserEmail

答案 1 :(得分:1)

您可以从https://github.com/rustd/AspnetIdentitySample下载样本。这基于ASP.NET and Web Tools 2013 Preview Refresh附带的ASP.NET MVC模板(仅支持英文版VS2013预览版)安装此预览刷新后,您可以对ASP.NET Web窗体和SPA应用程序执行相同的操作。 / p>

以下是运行此项目的步骤

Open the solution
Build and run
Register a user ---- Notice that the user registration field only has user name and password
Let's ask for a birthdate option from the user while registering an account.
Goto Nuget Package Manager console and run "Enable-Migrations"
Goto Models\AppModel.cs and uncomment BirthDate property in the MyUser class
Goto Models\AccountViewModels.cs and uncomment BirthDate property in RegisterViewModel
Goto AccountController and in Register Action and have the following code var user = new MyUser() { UserName = model.UserName,BirthDate=model.BirthDate }; //var user = new MyUser() { UserName = model.UserName };
Goto Views\Account\Register.cshtml and uncomment the HTML markup to add a BirthDate column
Goto Nuget Package Manager console and run "Add-Migration BirthDate"
Goto Nuget Package Manager console and run "Update-Database"
Run the application
When you register a user then you can enter BirthDate as well

答案 2 :(得分:1)

UserStore和User类可以使基于EF的实现更容易,但您可以随时删除并实现自己的自定义IUserStore并传入您自己的DbContext。

如果需要,我可以提供更详细的示例。