All I wanted was to move the logic from the razor pages / model into a Controller because it ruins the organization of my project. I have a class that derives from IdentityUser.
This means that I should replaces the usings in the razor pages with injections through the controller's constructor, for some reason it's failing and I don't know why. I tried everything I could think of - the UserStore, the UserManager always worked fine even with custom IdentityUser classes such as mine. Initially I tested with the razor pages and UserManager<ForumUser>worked, as well as the SignInManager and the UserStore<ForumUser>.
When I moved them into the controller I started getting errors of this sort
InvalidOperationException: Unable to resolve service for type 'EweForum.Infrastructure.Data.Extensions.ForumUserStore' while attempting to activate 'EweForum.Controllers.AccountController'.
or you can replace the ForumUserStore with UserStore<ForumUser> etc. I created custom ones. Still doesn't work.
My program.cs has the AddDefaultIdentity method with the correct user - my derived one, not the 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();
None of my solutions work and I don't understand what I am missing.
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)
{
}
}
The error message tells you that the ASP.NET Dependency Injection container doesn't know how to resolve
ForumUserStore. Try adding it as a service: