简体   繁体   中英

Add new user and role to asp.net identity sample?

I have installed asp.net identity sample https://www.nuget.org/packages/Microsoft.AspNet.Identity.Samples and trying to create a new role "SAdmin" with the user "Sadmin@example.com". The user is created but "Sadmin" gets the same role as "Admin"

I have modified IdentityConfig.cs to

    //Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
    public static void InitializeIdentityForEF(ApplicationDbContext db) {
        var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
        const string name = "admin@example.com";
        const string password = "Admin@123456";
        const string roleName = "Admin";

        const string Sname = "Sadmin@example.com";
        const string Spassword = "SAdmin@123456";
        const string SroleName = "SAdmin";

        //Create Super if it does not exist
        var Srole = roleManager.FindByName(SroleName);
        if (Srole == null)
        {
            Srole = new IdentityRole(roleName);
            var roleresult = roleManager.Create(Srole);
        }

        var Suser = userManager.FindByName(Sname);
        if (Suser == null)
        {
            Suser = new ApplicationUser { UserName = Sname, Email = Sname };
            var result = userManager.Create(Suser, Spassword);
            result = userManager.SetLockoutEnabled(Suser.Id, false);
        }

        // Add Suser to Role Admin if not already added
        var SrolesForUser = userManager.GetRoles(Suser.Id);
        if (!SrolesForUser.Contains(Srole.Name))
        {
            var result = userManager.AddToRole(Suser.Id, Srole.Name);
        }

        //Create Role Admin if it does not exist
        var role = roleManager.FindByName(roleName);
        if (role == null) {
            role = new IdentityRole(roleName);
            var roleresult = roleManager.Create(role);
        }

        var user = userManager.FindByName(name);
        if (user == null) {
            user = new ApplicationUser { UserName = name, Email = name };
            var result = userManager.Create(user, password);
            result = userManager.SetLockoutEnabled(user.Id, false);
        }

        // Add user admin to Role Admin if not already added
        var rolesForUser = userManager.GetRoles(user.Id);
        if (!rolesForUser.Contains(role.Name)) {
            var result = userManager.AddToRole(user.Id, role.Name);
        }
    }

The problem is in the code... The following code block is responsible

    if (Srole == null)
    {
    **Srole = new IdentityRole(roleName);**
    var roleresult = roleManager.Create(Srole);
    }

Change the highlighted line to

    **Srole = new IdentityRole(SroleName);**

That should solve it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM