簡體   English   中英

ASP.Net-MVC5身份驗證

[英]ASP.Net - MVC5 Authentication

關於身份驗證在給定模板中的工作方式,我只有幾個基本問​​題。 當我在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的數據庫,其中沒有表,我將如何為此創建模型,控制器和表? 我只是看不到模型和數據庫之間的連接以及表的結構(或者它想知道要從表中獲取數據時使用什么名稱)的方式。

謝謝

連接分為兩部分。 第一個是IdentityDbContext<TUser> ,它是為您IdentityDbContext<TUser>的上下文繼承的。 它具有以下屬性:

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

這指示Entity Framework為IdentityRoleTUser創建表,這是在定義IdentityDbContext<>子類時填寫的泛型。 默認值為ApplicationUser 不太明顯的是,與這兩個相關的任何類都會隨手攜帶並獲得自己的表,因此我們需要查看ApplicationUser 但是, ApplicationUser就是您要添加的內容。 從某種意義上說,它就是您的用戶,它隨您而創造。 真正的實質來自它繼承自IdentityUser的事實,后者是連接的第二部分。 IdentityUser具有以下導航屬性(與其他類的關系):

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

因此,在這兩者之間,您將獲得IdentityUserIdentityRoleIdentityUserClaimIdentityUserLoginIdentityUserRole 有你的五張桌子。 但是,您可能會問,這些不是表名。 這是為什么? 好吧,那是Microsoft,沒有充分的理由只是自定義生成的表名。 好吧,原因很可能是因為舊版本的ASP.NET身份驗證方案具有這種樣式的表名,因此它們只是以這種方式繼續使用。 我想這是否是“良好”的理由值得商bat。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM