ive添加了ASP.NET身份,并且我已经设置了启动类以添加角色(如果不存在(并添加默认的Superuser。
我的问题是,当应用程序运行时,它会根据代码在数据库中创建角色,但没有添加超级用户详细信息。
我缺少什么?
我在这里遵循说明:https://code.msdn.microsoft.com/aspnet-mvc-5-security-and-44cbdb97
我还删除了Aspnetroles表中的行,并检查了它们是否在启动时重新创建,它们表明该功能在启动时都调用。
在startup.cs文件中
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
createRolesandUsers();
}
private void createRolesandUsers()
{
ApplicationDbContext context = new ApplicationDbContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
// In Startup i am creating first Super Role and creating a default Super User
if (!roleManager.RoleExists("Super"))
{
// first we create Admin role
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Super";
roleManager.Create(role);
//Here we create a Admin super user who will maintain the website
var user = new ApplicationUser();
user.UserName = "Admin";
user.Email = "admin@transsafe.co.nz";
string userPWD = "L1llo1.";
var result = UserManager.Create(user, userPWD);
//Add default User to Role Admin
if (result.Succeeded)
{
var result1 = UserManager.AddToRole(user.Id, "Super");
}
}
// creating Creating Manager role
if (!roleManager.RoleExists("Administrator"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Administrator";
roleManager.Create(role);
}
// creating Creating Manager role
if (!roleManager.RoleExists("Manager"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Manager";
roleManager.Create(role);
}
// creating Creating Employee role
if (!roleManager.RoleExists("User"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "User";
roleManager.Create(role);
}
}
}
我希望用户" admin"在Aspnetusers表中以及Aspnetroles表中创建的角色。
试图更改以下方式:
var user = new ApplicationUser();
user.UserName = "Admin";
user.Email = "admin@transsafe.co.nz";
以下内容:
var user = new ApplicationUser { UserName = "Transsafe", UserOrganisation = "Transsafe" };
起初效果很好。然后,当我重新测试时,它再次失败。对任何想法开放。