自定义 'ApplicationUser' 以在 ASP.NET Angular 模板中保存更多用户数据(dotnet core 3.0)

Customizing the 'ApplicationUser' to hold more user data in ASP.NET Angular template (dotnet core 3.0)

我目前正在开发一个 ASP.NET Web 应用程序,以 angular 作为前端。作为基础,VisualStudio 2019 中针对 ASP.NET angular 提供的新模板具有个人身份验证。

这个 运行 在 dotnet core 3.0 Preview 4 上。

我想做的是将一些用户数据添加到默认的 ApplicationUser。 假设我的 ApplicationUser 是一名学生,我希望该学生参加多个 classes,并且每个 classes 都有多个作业。

应用程序用户 --> * Class --> * 分配

为此,我为 Class 和作业创建了模式,并添加了这样的关系

ApplicationUser 中的示例:

public class ApplicationUser : IdentityUser
{ 
    public virtual ICollection<Class> Classes { get; set; }
}

Class中的示例:

public class Class
{
    public int ID { get; set; }
    public string Name { get; set; }
    public double CurrentGrade
    {
        get
        {
            if (Assignments.Count == 0)
            {
                return -1;
            }

            double grade = 0;
            foreach (Assignment a in Assignments)
            {
                grade += (a.Weight * a.Score);
            }

            return grade;
        }
    }

    public virtual ICollection<Assignment> Assignments { get; set; }

    public Class()
    {
        Assignments = new List<Assignment>();
    }

}

作业中的示例:

public class Assignment
{
    public int ID { get; set; }

    public virtual Class FromClass { get; set; }

    public string Name { get; set; }
    public double Weight
    {
        get { return Weight; }
        set
        {
            if (value <= 0 || value > 100)
            {
                throw new ArgumentException("The weight of the assignment should be between 0 and 100");
            }
            Weight = value;
        }
    }
    public double Score
    {
        get { return Score; }
        set
        {
            if (value <= 0 || value > 100)
            {
                throw new ArgumentException("The score of the assignment should be between 0 and 100");
            }

            Score = value;
        }
    }

    public Assignment()
    {

    }

    public Assignment(string name, double weight, double score)
    {
        this.Name = name;
        this.Weight = weight;
        this.Score = score;
    }

}

这样我就添加了虚拟标签来定义 classes 之间的关系。

最后,修改了 ApplicationDbContext class 以便生成新实体的 tables(根据我的理解)

public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
{

    public virtual DbSet<Class> Classes { get; set; }

    public virtual DbSet<Assignment> Assignments { get; set; }

    public ApplicationDbContext(
            DbContextOptions options,
            IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
    {

    }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Invoke the Identity version of this method to configure relationships 
        // in the AspNetIdentity models/tables
        base.OnModelCreating(modelBuilder);

        // Add a configuration for our new table.  Choose one end of the relationship
        // and tell it how it's supposed to work
        modelBuilder.Entity<ApplicationUser>().HasMany(e => e.Classes);        // ApplicationUser has many Classes

        modelBuilder.Entity<Class>().HasMany(e => e.Assignments); 
    }


}

我相信这就是实现这一目标所需要的一切,但也许还缺少一些东西。

我现在的主要问题是注册页面。模板项目已经内置了登录和注册功能。我想修改注册页面,这样当用户注册时,它可以指定当前登录的 classes。这样当它的 ApplicationUser 是创建并保存在身份 table 中,用户(学生)正在使用的 classes 也被创建并保存在数据库中。

需要修改的模板注册页面:

问题是在模板中找不到处理此问题的 html/typescript 文件。谁知道我该如何修改此页面以适应应用程序? (去掉 'User another service to register' 通用文本也很好。)

您可以通过搭建 "Identity".

脚手架来覆盖默认登录页面

Right-click 在解决方案资源管理器中的项目上,然后 select:添加->新建脚手架项->标识

然后点击添加。您可以选择要覆盖的页面(或只选择全部)。这些页面将在 "Areas/Identity" 文件夹中创建。