繁体   English   中英

如何使用ASP.net Core中已经存在的用户表在项目上添加身份验证?

[英]How can I add authentication on my project using an already exist user table in ASP.net core?

我无法在已经存在的用户表上构建身份,并且无法在身份中修改用户表

我创建了模型,然后生成了控件和视图。

https://imgur.com/Q6yhKgZ.png

然后,我尝试通过添加新的脚手架项目来创建新的标识,但是无法使用已经存在的用户表

https://imgur.com/V5YCuyz.png

我还尝试创建一个新项目,首先创建Identity,然后通过添加一些列来修改Identity用户表,然后在相同的上下文中创建我的表,但是我找不到用于修改它并创建关系的用户模型。与其他实体。

[Table("User")]
public class User
{
    [Key]
    public int Id { get; set; }

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

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


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


    [Required]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    [StringLength(20, MinimumLength = 6)]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Confirm Password")]
    [DataType(DataType.Password)]
    [Compare("Password", ErrorMessage = "Confirm password doesn't match, Type again !")]
    [NotMapped]
    public string ConfirmPassword { get; set; }

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

    [Required]
    [Display(Name = "Birth Date")]
    [DataType(DataType.Date)]
    [CustomDate(ErrorMessage = "Use Correct Birthdate")]
    public DateTime BirthDate { get; set; }

    public bool Eanble { get; set; }
    public string Permissions { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime CreateAt { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime UpdateAt { get; set; }

    [InverseProperty("Owner")]
    public List<Article> Articles { get; set; }


    [InverseProperty("Owner")]
    public List<Consultation> Consultations { get; set; }


    [InverseProperty("Owner")]
    public List<ConsultationComment> ConsultationComments { get; set; }
}

您使用了错误的方法。 为了向用户添加自己的自定义属性,您应该创建一个派生自Microsoft.AspNetCore.Identity.IdentityUser的类。 在这里,您可以添加自己的自定义属性,这些属性将添加到表中。 这是您可以执行的操作:

// using Microsoft.AspNetCore.Identity; is needed
public class ApplicationUser : IdentityUser
{
    public int Age { get; set; } 
}

现在,您应该创建一个从IdentityDbContext派生的类:

 public class AppIdentityDbContext : IdentityDbContext<ApplicationUser>
{
    public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

基本上,这是一个具有Identity将使用的2个DbSet的DbContext。

然后转到您的Startup.cs并在ConfigureServices内添加以下行:

services.AddIdentity<ApplicationUser, IdentityRole>()
   .AddEntityFrameworkStores<AppIdentityDbContext>();

现在,您可以构建项目并查看应用于数据库的更改。 您不能删除身份属性,但可以添加自己的属性。

暂无
暂无

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

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