标识"自定义用户数据"在 .NET 5 中不起作用



.NET 5;Visual Studio 2019,版本16.9.4

我创建了一个新的运行在.NET5上的ASP.NET Core MVC web应用程序。

当我运行应用程序并注册时,它会提示"应用迁移",然后使用标准标识表创建数据库。

然后,我删除数据库并添加一个ApplicationUser,其中包含以下自定义用户数据https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-5.0

<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.5" />
public class ApplicationUser : IdentityUser
{
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
}
public class AlbumDbContext : IdentityDbContext<ApplicationUser>
{
public AlbumDbContext(DbContextOptions<AlbumDbContext> options)
: base(options) {}
}

using Microsoft.AspNetCore.Identity;  
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<AlbumDbContext>()
.AddDefaultUI();

.AddDefaultIdentity是"ASP.NET核心Web应用程序(模型视图控制器("模板的默认值。

如果我试图将其更改为AddIdentity,我会得到一个错误

IServiceCollection不包含AddIdentity

添加迁移和更新数据库不会出错,并且两个ApplicationUser字段会添加到AspNetUsers表中。

现在,当我运行应用程序时,我收到一个错误:

InvalidOperationException:没有为类型"Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.IdentityUser]"注册的服务

我已经查看了关于这个问题的大量帖子,但没有找到解决方案。

是我遗漏了什么,没有记录,还是这是一个bug?能够添加客户数据是Identity的一个非常基本的功能。

引用的文章说更新Pages/Shared/_LoginPartial.chtml,它不存在,因为Identity是作为Razor类库提供的,所以我忽略了这一部分。

然而,有一个Views/Shared/_LoginPartial.chtml,如果我将IdentityUser更改为ApplicationUser,应用程序现在就会运行。

@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

最新更新