ASP.NET Core Identity does not inject UserManager

后端 未结 3 1604
挽巷
挽巷 2021-01-04 05:46

I\'ve got an older asp.net core identity database, and I want to map a new project (a web api) to it.

Just for the test, I copied the Models folder, and the Applicat

相关标签:
3条回答
  • 2021-01-04 06:03

    Do you have the app.UseIdentity(); call in the Configure method:

     public void Configure(IApplicationBuilder app, 
                           IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            /*...*/
            app.UseIdentity();
           /*...*/          
        }
    

    EDIT Do you also have this line before the services.AddIdentity<ApplicationUser, IdentityRole>() line?

     public void ConfigureServices(IServiceCollection services)
     {
            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                 options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
     }
    

    This should work OK. Also please check if ApplicationDbContext inherits from IdentityDbContext.

    0 讨论(0)
  • 2021-01-04 06:05

    DI container is unable to resolve a dependency. Add it to the services collection

    services.AddTransient<UserManager<ApplicationUser>>();
    services.AddTransient<ApplicationDbContext>();
    

    You should also familiarize yourself with the official documentation

    0 讨论(0)
  • 2021-01-04 06:06
    public void ConfigureServices(IServiceCollection services){
    ...
    var identityBuilder = services.AddIdentityCore<ApplicationUser>(user =>
                {
                    // configure identity options
                    user.Password.RequireDigit = true;
                    user.Password.RequireLowercase = false;
                    user.Password.RequireUppercase = false;
                    user.Password.RequireNonAlphanumeric = false;
                    user.Password.RequiredLength = 6;
                });
                identityBuilder = new IdentityBuilder(identityBuilder.UserType, typeof(IdentityRole), identityBuilder.Services);
                identityBuilder.AddEntityFrameworkStores<DbContext>().AddDefaultTokenProviders();
        ...
    }
    
    0 讨论(0)
提交回复
热议问题