C# 无法解析类型为';Microsoft.AspNetCore.Identity.SignInManager`试图激活';xxxxx.登录模型';

C# 无法解析类型为';Microsoft.AspNetCore.Identity.SignInManager`试图激活';xxxxx.登录模型';,c#,asp.net,asp.net-core,asp.net-identity,identityserver4,C#,Asp.net,Asp.net Core,Asp.net Identity,Identityserver4,我已经使用CLI构建了身份 dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design dotnet add package Microsoft.EntityFrameworkCore.Design 然后我列出所有的文件 dotnet aspnet-codegenerator identity --listFiles 我使用了logindotnet aspnet codegenerator标识--files=“Acc

我已经使用CLI构建了身份

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
然后我列出所有的文件

dotnet aspnet-codegenerator identity --listFiles
我使用了login
dotnet aspnet codegenerator标识--files=“Account.login”
现在我可以在区域部分看到login.cshtml文件的生成

现在当我导航到
https://localhost:5001/Identity/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DVotingApplication%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A5001%252Fauthentication%252Flogin-回调%26响应类型%3Dcode%26作用域%3DVotingApplicationAPI%2520openid%2520profile%26状态%3Daceb10c45488493b9a675c830327ecbf%26代码_质询%3DnU-0BBK2wjgOSEDdB4Qjy5T8f3qHWTlgu0UmMgVDEAU%26编码\质询\方法%3DS256%26响应\模式%3Dquery

我得到了一个例外

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'VotingApplication.Areas.Identity.Pages.Account.LoginModel'.
在startup.cs中,我有以下配置

services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
                .AddProfileService<ProfileService>();
由于架子工正在使用
IdentityUser
,如何使其与
ApplicationUser

\u LoginPartial.cshtml

@using Microsoft.AspNetCore.Identity
@using global::Data
@using global::Data.Domain
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

@{
    string returnUrl = null;
    var query = ViewContext.HttpContext.Request.Query;
    if (query.ContainsKey("returnUrl"))
    {
        returnUrl = query["returnUrl"];
    }
}

<ul class="navbar-nav">
    @if (SignInManager.IsSignedIn(User))
    {
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
        </li>
        <li class="nav-item">
            <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="/">
                <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
            </form>
        </li>
    }
    else
    {
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register" asp-route-returnUrl="@returnUrl">Register</a>
        </li>
        <li class="nav-item">
            <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login" asp-route-returnUrl="@returnUrl">Login</a>
        </li>
    }
</ul>
@使用Microsoft.AspNetCore.Identity
@使用全局::数据
@使用global::Data.Domain
@注入SignInManager SignInManager
@注入用户管理器用户管理器
@{
字符串returnUrl=null;
var query=ViewContext.HttpContext.Request.query;
if(query.ContainsKey(“returnUrl”))
{
returnUrl=查询[“returnUrl”];
}
}
    @if(SignInManager.IsSignedIn(用户)) {
  • 你好@User.Identity.Name!
  • 注销
  • } 其他的 {
  • 登记
  • 登录
  • }

您有一个依赖于ApplicationSignInManager的类。您需要注册ApplicationSignInManager

services.AddIdentity<User, Role>(identityOptions =>
        {
            // identityOptions.SignIn.RequireConfirmedPhoneNumber = true;
            // identityOptions.SignIn.RequireConfirmedEmail = true;
        })
        .AddUserManager<ApplicationUserManager>()      // <- use only if use have custom implementation
        .AddSignInManager<ApplicationSignInManager>(); // <- use only if use have custom implementation
services.AddIdentity(identityOptions=>
{
//identityOptions.SignIn.Required确认电话号码=真;
//identityOptions.SignIn.requireconfirmedail=true;
})
.AddUserManager

InvalidOperationException:在尝试激活“VotingApplication.Areas.Identity.Pages.Account.LoginModel”时,无法解析类型为“Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.Identity.IdentityUser]”的服务

似乎您将其命名为
ApplicationUser

根据您的描述和代码,我们可以发现您在
ConfigureServices
方法和
\LoginPartial.cshtml
文件中刚刚将
IdentityUser
替换为
ApplicationUser
,您似乎没有更新
Login.cshtml.cs
文件来将
IdentityUser
替换为
ApplicationUser
,这导致了问题

请确保使用
UserManager
SignInManager
LoginModel
类中插入UserManagerSignInManager的实例,如下所示

[AllowAnonymous]
public class LoginModel : PageModel
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly ILogger<LoginModel> _logger;

    public LoginModel(SignInManager<ApplicationUser> signInManager, 
        ILogger<LoginModel> logger,
        UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _logger = logger;
    } 
[AllowAnonymous]
公共类LoginModel:PageModel
{
私有只读用户管理器_UserManager;
专用只读签名管理器\u签名管理器;
专用只读ILogger\u记录器;
公共登录模型(SignInManager SignInManager,
ILogger记录器,
用户管理器(用户管理器)
{
_userManager=userManager;
_signInManager=signInManager;
_记录器=记录器;
} 

什么是ApplicationUserManager和ApplicationSignInManager,我没有这些的任何实现manager@SanJaisy,您可以对这两行进行注释。将选择默认实现。看起来您将
SignInManager
插入到
Login.cshtml.cs
文件中,但这应该是
SignInManager
请看你的其他代码。嗨@SanJaisy,关于这个案例有什么更新吗?你试过上面的解决方案吗?
[AllowAnonymous]
public class LoginModel : PageModel
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly ILogger<LoginModel> _logger;

    public LoginModel(SignInManager<ApplicationUser> signInManager, 
        ILogger<LoginModel> logger,
        UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
        _logger = logger;
    }