Skip to content

Injecting UserManager<> in class library #2069

@evindj

Description

@evindj

Hi All,
I have defined a class library that is used by other components of the application. The library contains my model classes and all of them implement a persist method that create a database record or update an existing record in the database. My User class extends the IdentityUser class the persist class add new users correctly but if the user already exist everything becomes trickier. I though of using the UpdateAsync Method of the UserManager class to update the user but I can't figure out a way to inject/ correctly create a UserManager inside my User c;lass.
Here is my services configuration `implementation:

`public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<User, IdentityRole>(config =>
            {
                config.User.RequireUniqueEmail = true;
            } ).AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            var builder = services.AddMvc();
            builder.AddMvcOptions(o => o.Filters.Add(new GlobalExceptionHandling()));
            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
            services.AddScoped<IUserManager, CustomUserManager>();
            services.AddTransient<UserManager<User>>();
            services.AddTransient<User>();
            services.Configure<AuthMessageSenderOptions>(Configuration);
        }

I defined a constructor to help inject a user manager as I'll do within a controller class by that does not seem to work. Please do you guys have any points how i could handle this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions