【问题标题】:ASP.Net - MVC5 AuthenticationASP.Net - MVC5 身份验证
【发布时间】:2014-05-15 14:18:57
【问题描述】:

关于身份验证如何在给定模板中工作,我有几个基本问​​题。当我在 Visual Studio 2013 中创建一个新的 MVC5 项目时,我看到其中有几种不同的类型,其中一种是 MVC,其中包括个人用户帐户。我创建了它,Visual Studio 为该项目创建了所有的脚手架。我可以登录和注册等。我的问题如下:

代码如何知道要使用哪些表:例如自动创建的表是:

但在模型帐户视图模型中: 我看到没有一个类使用任何表名:

namespace HelloLogin.Models
{
    public class ExternalLoginConfirmationViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }
    }

    public class ManageUserViewModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class LoginViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
}

我没有看到两者之间的联系以及如何使用此模型从数据库中获取信息

如果有人可以给我一个简单的例子(或教程的链接),我将如何将两者联系起来。假设我有一个名为:没有表的 helloLogin 数据库,我将如何为此创建模型、控制器和表?我只是没有看到模型和数据库之间的连接以及表是如何结构的(或者当它想从表中获取数据时它如何知道使用什么名称)。

谢谢

【问题讨论】:

  • 你看过控制器代码吗?您在示例中使用 EntityFramework 吗?这些只是在视图和控制器之间传递数据的视图模型。它们是表中数据的子集。

标签: c# asp.net sql asp.net-mvc database


【解决方案1】:

连接分为两部分。第一个是IdentityDbContext<TUser>,为您搭建的上下文继承自它。它具有以下属性:

public virtual IDbSet<IdentityRole> Roles { get; set; }
public virtual IDbSet<TUser> Users { get; set; }

这指示 Entity Framework 为 IdentityRoleTUser 创建表,这是您在定义 IdentityDbContext&lt;&gt; 的子类时填写的泛型。默认值为ApplicationUser。不太明显的是,与这两个相关的任何课程都会出现并获得自己的表格,因此我们需要查看ApplicationUser。但是,ApplicationUser 正是您添加的内容。它是你的用户,从某种意义上说,它是你创造的任何东西。真正的肉来自它继承自IdentityUser,这是连接的第二部分。 IdentityUser 具有以下导航属性(与其他类的关系):

public virtual ICollection<IdentityUserClaim> Claims { get; }
public virtual ICollection<IdentityUserLogin> Logins { get; }
public virtual ICollection<IdentityUserRole> Roles { get; }

因此,在这两者之间,您将获得 IdentityUserIdentityRoleIdentityUserClaimIdentityUserLoginIdentityUserRole 的表。这是你的五张桌子。但是,您可能会问,这些不是表名。这是为什么?好吧,那是微软,只是自定义生成的表名,没有任何理由。嗯,原因很可能是因为旧版本的 ASP.NET 身份验证方案具有这种风格的表名,所以他们只是继续这种方式。我想这是否是一个“好”的理由值得商榷。

【讨论】:

  • 啊,我知道信息是从哪里来的。对一个新人来说相当混乱。我正在考虑从 SQL 查询中提取数据并将其放入模型中并在视图中使用它。我不知道这是更好的方法,还是使用实体框架更好。你怎么看?我想我总是可以改变它。
  • 始终使用 ORM(实体框架),除非使用存储过程更有意义。即使这样,您也可以将存储过程集成到您的上下文中。
  • 所以如果我想说在我的注册中添加另一个字段,例如出生日期,你能举例说明我会如何做到这一点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 2020-06-18
相关资源
最近更新 更多