简体   繁体   English

没有注册 UserIdentity 类型的服务

[英]No service for type UserIdentity has been registered

I have a web application where I want to integrate ASP.NET Core Identity but after adding controllers and views and editing the _Layout.cshtml I get this error:我有一个 Web 应用程序,我想在其中集成 ASP.NET Core Identity,但在添加控制器和视图并编辑 _Layout.cshtml 后,出现此错误:

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.SignInManager`1[StrikeNet.EntityFramework.Entities.UserIdentity]' has been registered. InvalidOperationException:未注册类型“Microsoft.AspNetCore.Identity.SignInManager`1[StrikeNet.EntityFramework.Entities.UserIdentity]”的服务。

When I search for this error on Google or on here I only get the solution to rename all instances of IdentityUser to the name I have given, being UserIdentity.当我在 Google 或此处搜索此错误时,我只能得到将 IdentityUser 的所有实例重命名为我提供的名称的解决方案,即 UserIdentity。 Also, many solutions say I should find it in the _LoginPartial.cshtml file.此外,许多解决方案都说我应该在 _LoginPartial.cshtml 文件中找到它。

The problem is, I don't have a file in my solution called _LoginPartial.cshtml and when I use the ctrl + F search tool and search for IdentityUser I also get no results.问题是,我的解决方案中没有名为 _LoginPartial.cshtml 的文件,当我使用 ctrl + F 搜索工具并搜索 IdentityUser 时,我也没有得到任何结果。

Any ideas on what could be the remaining problem?关于剩下的问题可能是什么的任何想法?

我认为您忘记将 Identity 添加到 Startup.cs 配置中:

services.AddIdentity<User, Role>(); // if you have roles

The problem is, I don't have a file in my solution called _LoginPartial.cshtml问题是,我的解决方案中没有名为 _LoginPartial.cshtml 的文件

I would suggest you create the asp.net core application with Identity template : Create ASP.NET Core web application -->Change Authentication --> choose Individual User Accounts .我建议您使用身份模板创建 ASP.NET Core 应用程序:创建 ASP.NET Core Web 应用程序 --> 更改身份验证 --> 选择个人用户帐户。 Ater visual studio help creating application with identity enabled , you can check the dbcontext , pages and startup file to find difference with your exist application .在 Visual Studio 帮助创建启用身份的应用程序后,您可以检查 dbcontext、页面和启动文件以查找与现有应用程序的差异。 After copying the missing files , you can create your user entity inheriting from IdentityUser :复制丢失的文件后,您可以创建继承自IdentityUser用户实体:

public class UserIdentity: IdentityUser
{
}

Modify the ApplicationDbContext :修改ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<UserIdentity>
{
}

Modify Startup:修改启动:

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

And update the _LoginPartial :并更新 _LoginPartial :

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

You can also refer to this article for more details .您也可以参考这篇文章了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM