【问题标题】:aspnet.core Seeding data using UserManager<IdentityUser>aspnet.core 使用 UserManager<IdentityUser> 播种数据
【发布时间】:2020-01-26 11:18:40
【问题描述】:

我正在尝试基于 http://docs.identityserver.io/en/latest/quickstarts/8_aspnet_identity.html 并使用来自 https://github.com/IdentityServer/IdentityServer4/tree/master/samples/Quickstarts/8_AspNetIdentity 的代码作为我的模板来启动 Identity Server 实例,但我无法获取种子数据。

根据示例应用程序,我有我的 program.cs 和 startup.cs。当我打电话时

var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>(); 

来自SeedData.EnsureSeedData 方法,该方法紧随其后

var services = new ServiceCollection();
            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));

            services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

            using var serviceProvider = services.BuildServiceProvider();
            using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
            var context = scope.ServiceProvider.GetService<ApplicationDbContext>();
            context.Database.Migrate();

            var userMgr = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>();

我收到以下错误。

Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'.

如何为ILogger&lt;UserManager&lt;IdentityUser&gt;&gt;&gt; 注册服务?

我觉得我遗漏了一些明显的东西,但看不到它是什么。

【问题讨论】:

    标签: c# asp.net-core .net-core asp.net-core-mvc identityserver4


    【解决方案1】:

    这是我使用用户管理器播种数据的方式

    public static void Main(string[] args)
            {
                try
                {
                    var host = BuildWebHost(args);
                    using (var scope = host.Services.CreateScope())
                    {
                        var services = scope.ServiceProvider;
                        SeedData.Initialize(services).Wait();
                    }
    
                    host.Run();
                }
    }
    

    在种子数据类中

    private static async Task SeedUser(User user, ApplicationDbContext context, IServiceProvider serviceProvider,
                string[] roles)
            {
                var password = new PasswordHasher<User>();
                var hashed = password.HashPassword(user, "123456");
                user.PasswordHash = hashed;
                var userStore = new UserStore<User>(context);
    
                await userStore.CreateAsync(user);
                await EnsureRole(serviceProvider, user.Email, roles);
                await context.SaveChangesAsync();
            }
    

    你可以查看我的完整代码here

    如果您需要任何帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 2019-08-16
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多