简体   繁体   English

尝试激活“xxxxx.LoginModel”时无法解析“Microsoft.AspNetCore.Identity.SignInManager”类型的服务

[英]Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager` while attempting to activate 'xxxxx.LoginModel'

I have scaffolded the Identity using the CLI as我已经使用 CLI 为身份搭建了脚手架

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design

Then I list all the files然后我列出所有文件

dotnet aspnet-codegenerator identity --listFiles

I used the login dotnet aspnet-codegenerator identity --files="Account.Login" Now I can see in the Area section the login.cshtml file is generated我使用了登录dotnet aspnet-codegenerator identity --files="Account.Login"现在我可以在 Area 部分看到 login.cshtml 文件的生成

在此处输入图片说明

Now when I navigate to https://localhost:5001/Identity/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DVotingApplication%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A5001%252Fauthentication%252Flogin-callback%26response_type%3Dcode%26scope%3DVotingApplicationAPI%2520openid%2520profile%26state%3Daceb10c45488493b9a675c830327ecbf%26code_challenge%3DnU-0BBK2wjgOSEDdB4Qjy5T8f3qHWTlgu0UmMgVDEAU%26code_challenge_method%3DS256%26response_mode%3Dquery现在当我导航到https://localhost:5001/Identity/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DVotingApplication%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A5001%252Fauthentication%252Flogin-callback%26response_type%3Dcode%26scope%3DVotingApplicationAPI%2520openid%2520profile%26state%3Daceb10c45488493b9a675c830327ecbf%26code_challenge%3DnU-0BBK2wjgOSEDdB4Qjy5T8f3qHWTlgu0UmMgVDEAU%26code_challenge_method%3DS256%26response_mode%3Dquery

I get an exception as我得到一个例外

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'.

In the startup.cs I have the below configuration在 startup.cs 我有以下配置

services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
                .AddProfileService<ProfileService>();

Since I am using the ApplicationUser , I am not able to see the account controller when I do the scaffolder.由于我使用的是ApplicationUser ,因此在执行脚手架时看不到帐户控制器。

public class IdentityHostingStartup : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
    {
        builder.ConfigureServices((context, services) => {
            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<ApplicationDbContext>();
        });
    }
}

Since the scaffolder is using IdentityUser , how can I make this to work with ApplicationUser由于脚手架正在使用IdentityUser ,我怎样才能使它与ApplicationUser

_LoginPartial.cshtml _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>

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'. InvalidOperationException:尝试激活“VotingApplication.Areas.Identity.Pages.Account.LoginModel”时,无法解析“Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]”类型的服务。

It seems that you update the IdentityUser derived class with custom properties and name it as ApplicationUser .您似乎使用自定义属性更新IdentityUser派生类并将其命名为ApplicationUser

And based on your description and code, we can find that you just replaced IdentityUser with ApplicationUser in ConfigureServices method and _LoginPartial.cshtml file, you seems not update Login.cshtml.cs file to replace IdentityUser with ApplicationUser , which cause the issue.并根据您的描述和代码,我们可以发现你刚刚更换IdentityUserApplicationUserConfigureServices方法和_LoginPartial.cshtml文件,你似乎没有更新Login.cshtml.cs文件替换IdentityUserApplicationUser ,这导致的问题。

Please make sure you inject the instance of UserManager and SignInManager within LoginModel class using UserManager<ApplicationUser> and SignInManager<ApplicationUser> , like below.请确保您使用UserManager<ApplicationUser>SignInManager<ApplicationUser>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;
    } 

You have a class that dependents on ApplicationSignInManager.您有一个依赖于 ApplicationSignInManager 的类。 You need to register 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

you can find sample implementation here你可以在这里找到示例实现

暂无
暂无

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

相关问题 尝试激活“AuthenticateController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthenticateController' 尝试激活“Management.Controllers.RoleController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'Management.Controllers.RoleController' 尝试激活“AuthController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController' 尝试激活“WebShop.Controllers.User.UserController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'WebShop.Controllers.User.UserController' 尝试激活“ Controllers.AccountsController”时无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager”的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager hile attempting to activate 'Controllers.AccountsController' 尝试激活时无法解析类型为“ Microsoft.AspNetCore.Mvc.IUrlHelper”的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Mvc.IUrlHelper' while attempting to activate 尝试激活图形 API 时无法解析类型“Microsoft.Identity.Web.ITokenAcquisition”的服务 - Unable to resolve service for type 'Microsoft.Identity.Web.ITokenAcquisition' while attempting to activate graph api 尝试激活时无法解析服务类型 - Unable to resolve service for type while attempting to activate 尝试激活“”时无法解析“”类型的服务 - Unable to resolve service for type '' while attempting to activate '' 尝试激活 Identity.IdentityUserManager 时无法解析 Identity.IdentityUserStore 类型的服务 - Unable to resolve service for type Identity.IdentityUserStore while attempting to activate Identity.IdentityUserManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM