将UserStore和UserManager注入到Controller中会抛出异常

问题描述 投票:0回答:1

我想要的只是将逻辑从剃刀页面/模型移动到控制器中,因为它破坏了我项目的组织。我有一个派生自 IdentityUser 的类。

这意味着我应该通过控制器的构造函数将剃刀页面中的使用替换为注入,由于某种原因它失败了,我不知道为什么。我尝试了我能想到的一切 - UserStore、UserManager 总是工作得很好,即使使用像我这样的自定义 IdentityUser 类也是如此。最初,我使用 razor 页面和 UserManager 以及 SignInManager 和 UserStore 进行了测试。

当我将它们移动到控制器中时,我开始收到此类错误

InvalidOperationException:尝试激活“EweForum.Controllers.AccountController”时无法解析类型“EweForum.Infrastruct.Data.Extensions.ForumUserStore”的服务。

或者您可以将 ForumUserStore 替换为 UserStore 等。我创建了自定义的。还是不行。

我的program.cs具有带有正确用户的AddDefaultIdentity方法 - 我的派生方法,而不是IdentityUser

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using EweForum.Data;
using EweForum.Infrastructure.Data.Models;
using Microsoft.CodeAnalysis.Options;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using EweForum.Infrastructure.Data.Extensions;

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'EweForumContextConnection' not found.");

builder.Services.AddDbContext<EweForumContext>(options =>
    options.UseSqlServer(connectionString));

builder.Services.AddDefaultIdentity<ForumUser>(options =>
{
    options.SignIn.RequireConfirmedAccount = false;
    options.Password.RequireDigit = false;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;



})
    .AddUserManager<ForumUserManager>()
    .AddUserStore<ForumUserStore>()
    .AddEntityFrameworkStores<EweForumContext>();

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();;

app.UseAuthorization();


app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
});

app.Run();

我的解决方案都不起作用,我不明白我错过了什么。

 public class ForumUser : IdentityUser


 public class AccountController : Controller
 {
     private readonly EweForumContext _context;
     private readonly SignInManager<ForumUser> _signInManager;
     private readonly ForumUserManager _userManager;
     private readonly ForumUserStore _userStore;
     private readonly Logger<RegisterModel> _logger;


     public AccountController(EweForumContext context,
                             SignInManager<ForumUser> signInManager,
                             ForumUserManager userManager,
                             ForumUserStore userStore,
                             Logger<RegisterModel>logger)
     {
         _context = context;
         _signInManager = signInManager;
         _userManager = userManager; 
         _userStore = userStore;
         _logger = logger;

     }

 public class ForumUserManager : UserManager<ForumUser>
 {

     public ForumUserManager(IUserStore<ForumUser> store,
         IOptions<IdentityOptions> optionsAccessor,
         IPasswordHasher<ForumUser> passwordHasher,
         IEnumerable<IUserValidator<ForumUser>> userValidators,
         IEnumerable<IPasswordValidator<ForumUser>> passwordValidators,
         ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors,
         IServiceProvider services, ILogger<UserManager<ForumUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
     {
     }
 }

 public class ForumUserStore : UserStore<ForumUser>
 {
     public ForumUserStore(EweForumContext context, IdentityErrorDescriber describer = null) : base(context, describer)
     {
     }
 }
asp.net asp.net-mvc dependency-injection entity-framework-6 asp.net-identity
1个回答
0
投票

错误消息告诉您ASP.NET 依赖注入容器不知道如何解析

ForumUserStore
。尝试将其添加为服务:

builder.Services.AddSingleton<ForumUserStore>();
© www.soinside.com 2019 - 2024. All rights reserved.