没有注册类型为UserIdentity的服务

问题描述 投票:0回答:2

我有一个Web应用程序,我想在其中集成ASP.NET Core身份,但是在添加控制器和视图并编辑_Layout.cshtml之后,出现此错误:

InvalidOperationException:没有注册类型为'Microsoft.AspNetCore.Identity.SignInManager`1 [StrikeNet.EntityFramework.Entities.UserIdentity]'的服务。

[当我在Google或此处上搜索此错误时,我只能得到将IdentityUser的所有实例重命名为我提供的名称,即UserIdentity的解决方案。另外,许多解决方案都说我应该在_LoginPartial.cshtml文件中找到它。

问题是,我的解决方案中没有名为_LoginPartial.cshtml的文件,当我使用ctrl + F搜索工具并搜索IdentityUser时也没有得到结果。

关于剩下的问题有什么想法?

c# asp.net-mvc visual-studio asp.net-core asp.net-core-identity
2个回答
0
投票
services.AddIdentity<User, Role>(); // if you have roles

0
投票
问题是,我的解决方案中没有名为_LoginPartial.cshtml的文件
我建议您使用身份模板创建asp.net核心应用程序:创建ASP.NET Core Web应用程序->更改身份验证->选择“单个用户帐户”。 Ater visual Studio帮助创建启用了身份的应用程序,您可以检查dbcontext,页面和启动文件以查找与现有应用程序的区别。复制丢失的文件后,您可以创建继承自IdentityUser的用户实体:

public class UserIdentity: IdentityUser { }

修改ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<UserIdentity>
{
}

修改启动:

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<UserIdentity>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();

并更新_LoginPartial:

@inject SignInManager<UserIdentity> SignInManager
@inject UserManager<UserIdentity> UserManager

您也可以参考this article了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.