0

我正在学习 C# 和 dotnet core,我目前正在研究模板

    dotnet new webapp --auth Individual -o WebApp1

但是,它在幕后为我做了很多我不明白的事情。

我正在翻阅代码以查找如何创建和处理登录视图,但没有这样的运气。目前,我正在尝试在此模板中给定的数据库中添加一列,如下所示:

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
4

2 回答 2

0

IdentityUser是身份的基本“用户”类,因此它正在填充它,因此您不需要额外的努力。如果您不想自定义用户,那很好,但既然您显然这样做了,只需创建您自己的派生自的类IdentityUser

public class MyUser : IdentityUser
{
    public string Foo { get; set; }
}

然后,使用您的自定义用户类作为类型参数来代替IdentityUser

services.AddDefaultIdentity<MyUser>()
于 2019-02-11T15:52:52.773 回答
0

dotnet 核心团队决定在 Razor 库中抽象其默认 UI 以进行身份​​验证,查找依赖项 -> SDK -> Microsoft.AspNetCore.App -> Microsoft.AspNetCore.Identity.UI

在此处查看此软件包的代码

这应该让您了解背景中实际发生的事情。

至于扩展用户模型

public class CustomUser : IdentityUser
{
     //custom properties
}

然后,您要配置身份中间件以将其识别为用户的主要模型。

services.AddDefaultIdentity<CustomUser>();

不要忘记更新您的数据库继承,以便创建正确的表。

public class ApplicationDbContext : IdentityDbContext<CustomUser> 
{
    ...
}
于 2019-02-11T16:02:12.910 回答