简体   繁体   English

ASP.Net-MVC5身份验证

[英]ASP.Net - MVC5 Authentication

Just a few basic questions I have about how authentication works in the template given. 关于身份验证在给定模板中的工作方式,我只有几个基本问​​题。 When I create a new MVC5 project in visual studio 2013 I see that there are several different types of them one of them being MVC which includes individual user accounts. 当我在Visual Studio 2013中创建新的MVC5项目时,我看到它们有几种不同类型,其中一种是MVC,其中包括单个用户帐户。 I create it and visual studio creates all of the scaffolding for the project. 我创建了它,Visual Studio创建了该项目的所有脚手架。 I can login and register etc. My questions are the following: 我可以登录并注册等。我的问题如下:

How does the code know which tables to use: for example the tables that were automatically created are: 代码如何知道要使用的表:例如,自动创建的表为: 表

But in the model account view model: I see that none of the classes use any of the tables names: 但是在模型帐户视图模型中:我看到没有一个类使用任何表名:

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; }
    }
}

I am not seeing the connection between the two and how one would get information from the database using this model 我没有看到两者之间的联系,以及如何使用这种模型从数据库中获取信息

If anyone could please give me a simple example (or a link to a tutorial) of how I would connect the two. 如果有人可以,请给我一个简单的示例(或指向教程的链接),说明如何将两者联系起来。 Lets say I have an database called: helloLogin with no tables, how would I create the model, controller, and the tables for this? 可以说我有一个名为:helloLogin的数据库,其中没有表,我将如何为此创建模型,控制器和表? I just don't see the connection between the model and the database and how the tables are structures (or how it knows what name to use when it wants to get data from the table). 我只是看不到模型和数据库之间的连接以及表的结构(或者它想知道要从表中获取数据时使用什么名称)的方式。

Thanks 谢谢

The connection is in two parts. 连接分为两部分。 The first is IdentityDbContext<TUser> , which the context that was scaffolded for you inherits from. 第一个是IdentityDbContext<TUser> ,它是为您IdentityDbContext<TUser>的上下文继承的。 It has the following properties: 它具有以下属性:

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

This instructs Entity Framework to create tables for IdentityRole and TUser , which is a generic you fill in when you define your subclass of IdentityDbContext<> . 这指示Entity Framework为IdentityRoleTUser创建表,这是在定义IdentityDbContext<>子类时填写的泛型。 The default is ApplicationUser . 默认值为ApplicationUser What isn't quite so obvious is that any classes related to these two come along for the ride and get their own tables as well, so we need to look at ApplicationUser . 不太明显的是,与这两个相关的任何类都会随手携带并获得自己的表,因此我们需要查看ApplicationUser However, ApplicationUser is just what you add to it. 但是, ApplicationUser就是您要添加的内容。 It's your user, in the sense that it's whatever you make it. 从某种意义上说,它就是您的用户,它随您而创造。 The real meat comes from the fact that it inherits from IdentityUser , which is part two of the connection. 真正的实质来自它继承自IdentityUser的事实,后者是连接的第二部分。 IdentityUser has the following navigation properties (relationships to other classes): IdentityUser具有以下导航属性(与其他类的关系):

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

So between the two, you get tables for IdentityUser , IdentityRole , IdentityUserClaim , IdentityUserLogin , and IdentityUserRole . 因此,在这两者之间,您将获得IdentityUserIdentityRoleIdentityUserClaimIdentityUserLoginIdentityUserRole There's your five tables. 有你的五张桌子。 But, you are probably asking, those aren't the table names. 但是,您可能会问,这些不是表名。 Why is that? 这是为什么? Well, that's Microsoft, just customizing the generated table names for no good reason. 好吧,那是Microsoft,没有充分的理由只是自定义生成的表名。 Well, the reason is most likely because older versions of ASP.NET authentication schemes had table names in that style, so they just continued on that way. 好吧,原因很可能是因为旧版本的ASP.NET身份验证方案具有这种样式的表名,因此它们只是以这种方式继续使用。 I suppose it's debatable whether that's a "good" reason or not. 我想这是否是“良好”的理由值得商bat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM