【问题标题】:Default SuperUser not adding to database on startup默认超级用户在启动时未添加到数据库
【发布时间】:2019-09-23 15:10:08
【问题描述】:

我已将 asp.net 身份添加到我的应用程序中,并且我已设置我的启动类以添加角色(如果不存在)并添加默认超级用户。

我的问题是,当应用程序运行时,它会根据代码在数据库中创建角色,但没有添加超级用户详细信息。

我错过了什么?

我按照这里的说明进行操作: 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 表中创建的角色中。

【问题讨论】:

    标签: c# asp.net model-view-controller asp.net-identity


    【解决方案1】:

    试图改变这一点:

    var user = new ApplicationUser();
    user.UserName = "Admin";
    user.Email = "admin@transsafe.co.nz";
    

    到此为止:

    var user = new ApplicationUser { UserName = "Transsafe", UserOrganisation = "Transsafe" };
    

    一开始效果很好。然后当我重新测试它再次失败。接受任何想法。

    【讨论】:

      猜你喜欢
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多