1

我正在使用身份 3。我想在启动时为管理员用户播种。

我已将字符串 id 转换为“int”id,以github 上的SwiftCourier为例。标识 3 在某些方面有所不同,这适用于 SwiftCourier 示例中所示的标识的“int”设置,与标识 2和“int”标识的设置相比。

我通过在我的用户后面放置“int”来做到这一点:

public class User : IdentityUser<int>{...}

在我的角色之后:

public class Role : IdentityRole<int>

在配置服务的 startup.cs 中,我输入了这个:

            services.AddIdentity<User, Role>(options => options.User.AllowedUserNameCharacters = null)
            .AddEntityFrameworkStores<JobsDbContext, int>()
            .AddUserStore<UserStore<User, Role, JobsDbContext, int>>()
            .AddRoleStore<RoleStore<Role, JobsDbContext, int>>()
            .AddDefaultTokenProviders();

然后我用它来播种角色,它适用于整数 id:

public static void EnsureRolesCreated(this IApplicationBuilder app)
    {
        var context = app.ApplicationServices.GetService<JobsDbContext>();
        if (context.AllMigrationsApplied())
        {
            var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();

            var Roles = new List<Role>();

            Roles.Add(new Role { Name = "Admin", Description = "Users able to access all areas" });
            Roles.Add(new Role { Name = "Technician", Description = "Users able to access just the schedule" });

            foreach (var role in Roles)
            {
                if (!roleManager.RoleExistsAsync(role.Name.ToUpper()).Result)
                {
                    roleManager.CreateAsync(role);
                }
            }
        }
    }

到目前为止一切都很好......我只想从一个管理员用户开始,这就是我失败的地方......

我使用了@Guy 的以下 Stackoverflow示例

我正在用这条线与 UserStore 作斗争:

await new UserStore<User>(context).CreateAsync(userWithRoles.User);

它在“用户”上失败,这是我刚刚重命名的 ApplicationUser。

我得到的错误是:

The type 'JobsLedger.Models.Identity.User' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'JobsLedger.Models.Identity.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser<string>'.

我认为它似乎在抱怨 id 是“int”这一事实

在这方面,如何使 UserStore 与“int”一起工作?如上所示,它在 Startup.cs 中设置为 int ..

如果我必须创建一个与“int”一起使用的自定义“UserStore”..你如何在 Identity 3 中做到这一点?

4

1 回答 1

2

它可能不是最优雅的解决方案,但这有效......

我的目标是创建一个管理员用户并添加到管理员角色中。我在避免使用带有整数 ID 的 UserStoreand 的同时解决了这个问题。

using JobsLedger.DAL;
using JobsLedger.Models.Identity;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace JobsLedger.Models.Seeding
{
    public static class SeedAdminUser
    {
        public static async void EnsureAdminUserCreated(this IApplicationBuilder app)
        {
            var context = app.ApplicationServices.GetService<JobsDbContext>();
            if (context.AllMigrationsApplied())
            {
                var userManager = app.ApplicationServices.GetService<UserManager<User>>();
                var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();

                var user = new User
                {
                    UserName = "Admin",
                    NormalizedUserName = "ADMIN",
                    Email = "Email@email.com",
                    NormalizedEmail = "email@email.com",
                    EmailConfirmed = true,
                    LockoutEnabled = false,
                    SecurityStamp = Guid.NewGuid().ToString()
                };

                var password = new PasswordHasher<User>();
                var hashed = password.HashPassword(user, "password");
                user.PasswordHash = hashed;

                var result = await userManager.CreateAsync(user, user.PasswordHash);

                var AdminUser = await userManager.FindByNameAsync("Admin");

                if (AdminUser != null)
                {
                    if (roleManager.RoleExistsAsync("Admin").Result)
                    {
                        var RoleResult = await userManager.AddToRoleAsync(AdminUser, "Admin");
                    }
                }
            }
        }
    }
}

我还在“app.UseIdentity();”之后将此条目放入 StartUp.cs 类中:

app.EnsureAdminUserCreated();

...Models.Identity 中有“用户”类,这就是它包含在“使用”中的原因。

您还需要添加以下内容以使其正常工作:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;

最后,您需要根据上述问题添加角色。

于 2016-04-24T08:37:46.403 回答