簡體   English   中英

ASP.NET Core 2 導致 SignInManager 依賴項注入失敗

[英]SignInManager dependency injection fails with ASP.NET Core 2

我試圖在我的一個不是控制器的類中訪問 Microsoft.AspNetCore.Identity.SignInManager。

在 Startup.ConfigureServices() 我有:

services.AddIdentity<IdentityUser, IdentityRole>();

在我的課堂上,我有:

public class Auth : IAuth
{
    ...
    private readonly SignInManager<IdentityUser> _signInManager;

    public Auth(..., SignInManager<IdentityUser> signInManager)
    {
    }
}

我得到這個例外:

處理請求時發生未處理的異常。 InvalidOperationException: 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.AspNetUserManager 1[Microsoft.AspNetCore.Identity.IdentityUser]”時無法解析類型“Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.AspNetUserManager的服務.

請注意,我什至沒有在構造函數中引用傳遞的SignInManager對象(我是,但已將其刪除以簡化)。 如果我從構造函數中刪除SignInManager參數一切正常。

我在網上看到的所有相關示例最終都是這樣的情況,即在調用AddIdentity() SignInManager的指定用戶數據類型與構造函數參數中的用戶數據類型不匹配,但情況並非如此我的代碼。

閱讀相關文章和示例代碼使這看起來應該是一項簡單的任務。 我的構造函數的其他依賴注入參數工作得很好,只是如果我嘗試注入RoleManager同樣的問題(除了稍微不同的異常消息)。

添加身份后,您還沒有配置任何數據存儲。 文檔中你應該做這樣的事情:

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

但是,如果您使用的是 ASP.Net Core 2.1,請使用較新的版本:

services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

如果您使用的是 asp.net core 3.18 版本,請使用以下用戶

services.AddDefaultIdentity<IdentityUser>()
         .AddEntityFrameworkStores<ApplicationDbContext>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM