【问题标题】:How to add more fields to ASP.NET Core Identity?如何向 ASP.NET Core Identity 添加更多字段?
【发布时间】:2020-09-21 17:31:49
【问题描述】:

我正在使用 .NET 5 - 版本 5.0.100-rc.1.20452.10

我自定义表格AspNetUsers成功了,就这样

using Microsoft.AspNetCore.Identity;
using System;

namespace shadow.Models
{
    public class ApplicationUser : IdentityUser
    {
        public string Fullname { get; set; }
        public string AliasName { get; set; }
        public string SecondMobile { get; set; }
        public string About { get; set; }
        public string Avatar { get; set; }
        public DateTime? Created { get; set; }
        public DateTime? Modified { get; set; }
    }

}

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using shadow.Models;

namespace shadow.Data
{
    public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
        {

        }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }

        public virtual DbSet<ApplicationUser> ApplicationUsers { get; set; }

我正在寻找与表 AspNetRoles 类似的解决方案。我需要将文件Description 添加到表AspNetRoles,如何存档?请指导我如何在ApplicationDbContext 中进行更改。

我的努力:我试过了

using Microsoft.AspNetCore.Identity;

namespace shadow.Models
{
    public class ApplicationRole : IdentityRole
    {
        public string Description { get; set; }
    }

}

但是错误

ApplciationRole 转为ApplicationDbContext 时我感觉很难

【问题讨论】:

  • 在您配置ApplicationUser 的完全相同的方法调用中,您可以另外配置IdentityRole 的类。也许这个问答有帮助? stackoverflow.com/questions/50426278/…
  • 我更新了信息。将ApplciationRole 转为ApplicationDbContext 时我感觉很难
  • 错误说明了什么?
  • public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser, ApplicationRole, string&gt; 有效吗?其中最后一个参数string 是您的身份密钥的类型。

标签: asp.net-core .net-core asp.net-core-identity


【解决方案1】:

正如我们在sourcecode 中看到的,Microsoft.AspNetCore.Identity.EntityFrameworkCore 没有IdentityDbContext&lt;TUser, TRole&gt; 的类。它有 IdentityDbContext&lt;TUser&gt;IdentityDbContext&lt;TUser, TRole, TKey&gt; 类。因此,正如documentation

第 5 点所述

如果正在使用自定义 ApplicationRole 类,请更新该类以从 IdentityRole 继承。例如:

using System;
using Microsoft.AspNetCore.Identity;

public class ApplicationRole : IdentityRole<Guid>
{
    public string Description { get; set; }
}

更新 ApplicationDbContext 以引用自定义 ApplicationRole 类。例如,以下类引用了自定义 ApplicationUser 和自定义 ApplicationRole:

using System;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext :
    IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

上面最后一个参数Guid是用户和角色的主键类型,如here所述

【讨论】:

  • 在将 string 更改为 Guid 后,我发现错误 user-images.githubusercontent.com/1328316/…
  • 为了与文档保持一致,我将其保留为Guid。要使其正常工作,您可以将 ApplicationRole 类更改为 public class ApplicationRole : IdentityRole&lt;Guid&gt; 并将 ApplicationUser 类更改为 public class ApplicationUser : IdentityUser&lt;Guid&gt; 或者您可以再次将其设置为 string
猜你喜欢
  • 2014-06-28
  • 1970-01-01
  • 2021-08-23
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
相关资源
最近更新 更多