从 dotnet 新 web app 自定义 IdentityUser语言 - -auth Individual -o



我正在学习 C# 和 dotnet 核心,我目前正在处理模板

    dotnet new webapp --auth Individual -o WebApp1

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

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

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

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> 
{
    ...
}

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

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

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

services.AddDefaultIdentity<MyUser>()

最新更新