我正在使用Identity 3.我想在启动时为管理员用户提供种子。
我已将字符串ID转换为" int" ids在github上使用SwiftCourier作为示例。身份3在某些方面是不同的,这适用于" int"对于ids,如SwiftCourier示例中所示,与Identity 2和" int"的设置相比较IDS。
我已经通过放置" 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 example。
我正在努力使用这一行的UserStore:
await new UserStore<User>(context).CreateAsync(userWithRoles.User);
它失败了&#34;用户&#34;这是我刚刚重命名的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是&#34; int&#34;我想
如何让UserStore使用&#34; int&#34;在这方面?它在Startup.cs中设置为int,如上所示..
如果我必须创建一个自定义&#34; UserStore&#34;适用于&#34; int&#34; ..你如何在Identity 3中做到这一点?
答案 0 :(得分:2)
它可能不是最优雅的解决方案,但这有效......
我的目标是为管理员用户提供种子,并将其添加到管理员角色。我已经管理了这个,同时避免使用UserStoreand和整数ID。
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中有“User”类,这就是为什么它包含在“using”中。
您还需要添加以下内容才能使其正常工作:
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.DependencyInjection;
using System;
最后,您需要根据上述问题添加角色..