5

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

在 MVC 4 中,我将只使用我自己的 User 类,然后将 WebSecurity 初始化指向它,如下所示:

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

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

4

3 回答 3

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_Role、User_Claim、User_Login而不是 AspNetUsers、AspNetRoles、AspNetUsersRoles、AspNetUsersClaims、AspNetUserLogins。

当然,User表将包含其他字段:UserId(int 身份)、InternalUserFullNameUserEmail

于 2015-02-01T14:20:14.700 回答
1

您可以从https://github.com/rustd/AspnetIdentitySample下载示例。这是基于ASP.NET 和 Web Tools 2013 Preview Refresh附带的 ASP.NET MVC 模板(仅支持英文版的 VS2013 Preview)安装此 Preview Refresh 后,您可以对 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 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
于 2013-07-04T18:20:35.210 回答
1

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

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

于 2013-07-11T22:19:11.863 回答