【问题标题】:UserManager steps out of the function without throwing any ExcepcionUserManager 退出函数而不抛出任何异常
【发布时间】:2021-11-21 21:40:48
【问题描述】:

我正在尝试使用以下代码让我的 MVC 应用在数据库中创建初始预览用户:

private async Task CreateStartupUsers(UserManager<IdentityUser> userManager)
        {
            List<IdentityUser> admins = new List<IdentityUser>
            {
                new IdentityUser
                {
                    UserName = "admin@admin.com",
                    Email = "admin@admin.com",
                    EmailConfirmed = true
                }
            };

...

foreach (var admin in admins)
            {
                if (await userManager.FindByNameAsync(admin.UserName) != null)
                {
                    continue;
                }
                await userManager.CreateAsync(admin, DefaultPassword);
                IdentityUser user = await userManager.FindByNameAsync(admin.UserName);
                await userManager.AddToRoleAsync(user, "Admin");
            }

老实说,除了在非异步 public IConfiguration Configuration { get; } 中调用此函数之外,我在这里没有看到任何问题,但我认为它不能解释这样一个事实,即在调试期间调用 userManager 的指令没有执行并退出函数。

我还制作了一个非常相似的函数来创建初始用户角色,并且效果很好。

private async Task CreateRoles(RoleManager<IdentityRole> roleManager)
        {
            var roles = new List<IdentityRole>
            {
                new IdentityRole
                {
                    Name = "Admin"
                },
                new IdentityRole
                {
                    Name = "Employee"
                },
                new IdentityRole
                {
                    Name = "Customer"
                }
            };

            foreach (var role in roles)
            {
                if (await roleManager.RoleExistsAsync(role.Name)) continue;
                var result = await roleManager.CreateAsync(role);
                if (result.Succeeded) continue;

                throw new Exception($"Could not create '{role.Name}' role.");
            }
        }

这些函数在 StartUp.cs 中的 Configure 函数中调用,如下所示:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
                CreateRoles(roleManager);
                CreateStartupUsers(userManager);
            }

【问题讨论】:

  • 你能发布你的ConfigureService方法吗?

标签: c# asp.net-core asp.net-web-api asp.net-core-mvc


【解决方案1】:

由于Configuration 方法被同步调用并且它们的子方法异步调用,所以在Configuration 完成之前创建用户的方法没有完成。由于Configuration 必须为构建器返回void,因此我必须进行以下更改:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager<IdentityRole> roleManager, UserManager<IdentityUser> userManager)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
                CreateRolesAndUsersAsync(roleManager, userManager).GetAwaiter().GetResult();
            }

GetAwaiter()GetResult() 添加到包含我想要执行的两个异步方法的方法中,我已经确保它们的执行得到了预期的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2022-01-20
    • 2018-10-12
    • 2018-07-07
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多