简体   繁体   中英

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:

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.SignInManager`1[StrikeNet.EntityFramework.Entities.UserIdentity]' has been registered.

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. Also, many solutions say I should find it in the _LoginPartial.cshtml file.

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.

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

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 . 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 . After copying the missing files , you can create your user entity inheriting from IdentityUser :

public class UserIdentity: IdentityUser
{
}

Modify the 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 :

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

You can also refer to this article for more details .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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