asp.net-core - 为 AspNetUser 添加自定义字段
问题描述
我是 ASP.NET Core 的新手。如何在代码和 UI 中将自定义字段添加到我的用户模型?
当前的用户管理 UI 是这样的,但我想在模型、数据库和 UI 中添加一些额外的字段,例如信用或分数:
这是我的 AspNetUser 模型:
public partial class AspNetUsers
{
public AspNetUsers()
{
AspNetUserClaims = new HashSet<AspNetUserClaims>();
AspNetUserLogins = new HashSet<AspNetUserLogins>();
AspNetUserRoles = new HashSet<AspNetUserRoles>();
AspNetUserTokens = new HashSet<AspNetUserTokens>();
}
public string Id { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string ConcurrencyStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTimeOffset? LockoutEnd { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public double Credit { get; set; }
public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual ICollection<AspNetUserRoles> AspNetUserRoles { get; set; }
public virtual ICollection<AspNetUserTokens> AspNetUserTokens { get; set; }
}
解决方案
继承自 支持自定义用户数据IdentityUser
。通常将这种类型命名为 ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string CustomTag { get; set; }
}
定制的详细步骤可以在定制模型文档中找到。
在 ASP.NET Core 2.1 或更高版本中,Identity 作为 Razor 类库提供。有关详细信息,请参阅ASP.NET Core 项目中的脚手架标识。
搭建好脚手架后,可以在里面找到个人资料页面Areas.Identity.Pages.Account.Manage.Index.cshtml
,可以在里面添加实体,InputModel
自Index.cshtml.cs
定义里面的数据OnGetAsync
和OnPostAsync
功能。
推荐阅读
- django - 简单的 django DetailView 不起作用,如何将 2 个参数传递给 DetailView
- swift - 使用 R.Swift 库快速本地化枚举
- javascript - 如何避免在反应中对服务进行双重调用
- mysql - 在查询错误 2013 期间丢失与 MySQL 服务器的连接
- java - testcontainer启动后如何覆盖集成测试中application.properties中定义的端口?
- python - python for loop MIME sendmail 发出相同的内容
- hive - Google DataProc Hive 和 Presto 查询不起作用
- python-3.x - 缺少 scikit-learn 软件包
- python - Anaconda 中的 scipy.special 现在在哪里?
- javascript - 盖茨比:试图从前端获取图像路径但得到这个“类型错误:无法读取未定义的属性'图像'”