在 MVC 5 中使用密码播种管理员角色?
Seed an admin role with password in MVC 5?
我正在尝试使用 Entity Framework 在 MVC5 中植入管理员角色,但似乎我们不再使用 WebSecurity 和 SimpleRoleProvider。
我找到了一个很棒的 post 标题:MVC5 Seed Roles and Users。
我通读了这篇文章并尝试实施它,但我在两个地方遇到了问题。在提供的示例中,他们只展示了如何添加用户名,而该站点需要电子邮件和密码才能注册。
这是我以前在 MVC4 中的做法:
protected override void Seed(PhotoAlbum.Models.UsersContext context)
{
seedUserAccounts();
}
private void seedUserAccounts()
{
WebSecurity.InitializeDatabaseConnection("UsersContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);
SimpleRoleProvider roles = (SimpleRoleProvider)Roles.Provider;
if (!roles.RoleExists("admin"))
{
roles.CreateRole("admin");
}
SimpleMembershipProvider membership = (SimpleMembershipProvider)Membership.Provider;
if (membership.GetUser("account", false) == null)
{
membership.CreateUserAndAccount("account", "pass");
}
if (!roles.IsUserInRole("account", "admin"))
{
roles.AddUsersToRoles(new[] { "account" }, new[] { "admin" });
}
}
我的问题:
我想知道如何更改以下方法以允许使用电子邮件地址和密码。
protected override void Seed(BlogEngine.Models.ApplicationDbContext context)
{
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
if (!context.Users.Any(u => u.UserName == "myUsername"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "myUsername" };
manager.Create(user, "ChangeItAsap!");
manager.AddToRole(user.Id, "Admin");
}
}
正如 Steve Greene 在上面所说的那样,我已经差不多做到了……我确实做了一些改变。这是我的最终解决方案:
// makes an admin role if one doesn't exist
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
// if user doesn't exist, create one and add it to the admin role
if (!context.Users.Any(u => u.UserName == "admin"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "me@me.com", Email = me@me.com" };
manager.Create(user, "password");
manager.AddToRole(user.Id, "Admin");
}
我正在尝试使用 Entity Framework 在 MVC5 中植入管理员角色,但似乎我们不再使用 WebSecurity 和 SimpleRoleProvider。
我找到了一个很棒的 post 标题:MVC5 Seed Roles and Users。
我通读了这篇文章并尝试实施它,但我在两个地方遇到了问题。在提供的示例中,他们只展示了如何添加用户名,而该站点需要电子邮件和密码才能注册。
这是我以前在 MVC4 中的做法:
protected override void Seed(PhotoAlbum.Models.UsersContext context)
{
seedUserAccounts();
}
private void seedUserAccounts()
{
WebSecurity.InitializeDatabaseConnection("UsersContext", "UserProfile", "UserId", "UserName", autoCreateTables: true);
SimpleRoleProvider roles = (SimpleRoleProvider)Roles.Provider;
if (!roles.RoleExists("admin"))
{
roles.CreateRole("admin");
}
SimpleMembershipProvider membership = (SimpleMembershipProvider)Membership.Provider;
if (membership.GetUser("account", false) == null)
{
membership.CreateUserAndAccount("account", "pass");
}
if (!roles.IsUserInRole("account", "admin"))
{
roles.AddUsersToRoles(new[] { "account" }, new[] { "admin" });
}
}
我的问题:
我想知道如何更改以下方法以允许使用电子邮件地址和密码。
protected override void Seed(BlogEngine.Models.ApplicationDbContext context)
{
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
if (!context.Users.Any(u => u.UserName == "myUsername"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "myUsername" };
manager.Create(user, "ChangeItAsap!");
manager.AddToRole(user.Id, "Admin");
}
}
正如 Steve Greene 在上面所说的那样,我已经差不多做到了……我确实做了一些改变。这是我的最终解决方案:
// makes an admin role if one doesn't exist
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
// if user doesn't exist, create one and add it to the admin role
if (!context.Users.Any(u => u.UserName == "admin"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "me@me.com", Email = me@me.com" };
manager.Create(user, "password");
manager.AddToRole(user.Id, "Admin");
}