Home > Blockchain >  Custom UserManager Asp.net MVC
Custom UserManager Asp.net MVC

Time:12-06

Im trying to 'extend' the UserManager in an ASP.net MVC web app. I have read through a couple of previous posts regarding this, and I don't seem to be getting in correct.

Extending the UserManager

I have made a MyManager class that inherits UserManger:

namespace DHS_Intranet.Helpers
{

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

        }

        public async Task<bool> IsFirstLoginAsync(ApplicationUser user)
        { 
                return (bool)user.FirstLogin;            
        }

        public async Task<bool> IsPasswordChangeRequired(ApplicationUser user)
        {
            return user.ForcePasswordChange ?? true;
        }
    }
}

I have registered this in the Program.cs

builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
    options.Password.RequireNonAlphanumeric = false;
    options.User.RequireUniqueEmail = true;
    options.SignIn.RequireConfirmedAccount = false;
    options.SignIn.RequireConfirmedEmail = false;
})
    .AddRoles<IdentityRole>()
    .AddRoleManager<RoleManager<IdentityRole>>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders()
    .AddUserManager<MyManager>();

However when I try and register this in my controller public AccountController(MyManager<ApplicationUser> userManager, ...

I get the error: Error CS0308: The non-generic type 'MyManager' cannot be used with type arguments (CS0308)

I'm not sure what I am doing wrong.

Any pointers would be helpful.

Thanks

James

CodePudding user response:

I realised what I was doing wrong; my custom UserManager class should look like this:

namespace DHS_Intranet.Helpers
{

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

        }

        public async Task<bool> IsFirstLoginAsync(ApplicationUser user)
        { 
                return (bool)user.FirstLogin;            
        }

        public async Task<bool> IsPasswordChangeRequired(ApplicationUser user)
        {
            return user.ForcePasswordChange ?? true;
        }
    }
}

I didn't supply type to the MyManager

  • Related