简体   繁体   中英

Identity: Why is newly created role not being found?

I am working on an MVC application, and I have a form to create a role. The data from the form gets sent to the post create action method correctly. The signature of this method is:

[HttpPost]
public async Task<ActionResult> Create([Bind(Include = "Name")] AppRole appRole, string [] PrivsChecked)

Basically, this is meant to create a role, and attach the privileges selected for the role, as the user selected on the form. The issue is that when it comes to the assigning of privileges to the role, the newly created role isn't being found. The partial code is below:

    public async Task<ActionResult> Create([Bind(Include = "Name")] AppRole appRole, string [] PrivsChecked)
      {
      if (PrivsChecked == null || PrivsChecked.Length == 0)
         {
          return Json(new { status = "error", message = "At least one privilege must be selected." });
          }
          else
           {
              if (ModelState.IsValid)
                {
                 IdentityResult result
                                = await RoleManager.CreateAsync(new AppRole(appRole.Name));
                 if (result.Succeeded)
                            {
                                var approleToUpdate = db.IdentityRoles.Where(i => i.Id == appRole.Id).FirstOrDefault();
.
.
.

The appRoleToUpdate is assigned to null, however at least that code is reached, meaning the role creation was successful. The db context, declared as a global variable private AppIdentityDbContext db = new AppIdentityDbContext(); in the controller, is as follows:

using IdentityDevelopment.Models;
using Microsoft.AspNet.Identity;

namespace IdentityDevelopment.Infrastructure
{
    public class AppIdentityDbContext : IdentityDbContext<AppUser>
    {

        public AppIdentityDbContext() : base("IdentityDb") { }

        static AppIdentityDbContext()
        {
            Database.SetInitializer<AppIdentityDbContext>(new IdentityDbInit());
        }

        public static AppIdentityDbContext Create()
        {
            return new AppIdentityDbContext();
        }


        public System.Data.Entity.DbSet<IdentityDevelopment.Models.AppRole> IdentityRoles { get; set; }

        public System.Data.Entity.DbSet<IdentityDevelopment.Models.AppPrivilege> AppPrivileges { get; set; }
    }


    public class IdentityDbInit : NullDatabaseInitializer<AppIdentityDbContext>
    {
    }

}

The strange thing is that very similar code exists in the Edit method as well, and in that case, the appRoleToUpdate is found. What can I do to get the newly created role to be found within the same method? Does this have anything to with the async nature of the method?

Thank you.

You created the new role with a new name, but you don't really know its ID yet. Try checking for the name (make sure it is unique though), that should return you your role:

var approleToUpdate = db.IdentityRoles.Where(i => i.Name == appRole.Name).FirstOrDefault();

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