如何配置用户上下文/表



随着新的ASP.NET MVC 5预览版的发布,我如何配置Users上下文/表?

在MVC 4中,我只需要使用我自己的User类,然后将WebSecurity初始化指向它,tike this:

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

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

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


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、阿斯pNetUsersClaims、AspNetUserLogins。

当然,User表将包含其他字段:UserId(int identity)、InternalUserFullName用户电子邮件

UserStore和User类是为了使基于EF的实现更容易,但您始终可以下拉并实现自己的自定义IUserStore,并传入自己的DbContext。

如果你需要,我可以提供一个更详细的例子。

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

以下是运行此项目的步骤

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 ModelsAppModel.cs and uncomment BirthDate property in the MyUser class
Goto ModelsAccountViewModels.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 ViewsAccountRegister.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

相关内容

  • 没有找到相关文章

最新更新