asp.net-identityasp.net5

How do I add a role/claims dynamically to the Asp.Net identity tables in Asp.net5/EF7?


I am trying to create the WebAPI service for providing security using Asp.Net Identity in Asp.Net5/EntityFramework7. I have created a web application project using Asp.Net 5 templates. It has provided me with the template code for managing users. Now I am trying to do the same for roles/claims.

I have created a ApplicationRole class inheritingting the IdentityRole.

 public class ApplicationRole: IdentityRole
    {
        public ApplicationRole() : base() { }
        public ApplicationRole(string name, string description) : base(name)
        {
            this.Description = description;
        }
        public virtual string Description { get; set; }
    }

My Web API controller class method to add a new role is like below:

 [HttpPost("CreateRole", Name = "CreateRole")]
        public async Task CreateRole([FromBody]string role)
        {
            var applicationRole = new ApplicationRole(role, role);
            var idResult = await _roleManager.CreateAsync(applicationRole);
            if (idResult.Succeeded)
            {
                _logger.LogInformation(3, "Role Created successfully");
            }
            else
            {
                var resp = new HttpResponseMessage()
                {
                    Content = new StringContent("Internal error occurred"),
                    ReasonPhrase = "Internal error occurred",
                    StatusCode = HttpStatusCode.BadRequest
                };
                throw new HttpResponseException(resp);
            }
        }

When I try to execute this code, the appication crashes with the exception

 "A database operation failed while processing the request.
        ArgumentNullException: Value cannot be null.
         Parameter name: entityType 
        There are pending model changes for ApplicationDbContext
        Scaffold a new migration for these changes and apply them to the database from the command line:

        > dnx ef migrations add [migration name] 
        > dnx ef database update
    "

It crashes in the line var idResult = await _roleManager.CreateAsync(applicationRole); The Quick watch values for the applicationRole object variable can be found in the attached snapshot. When the above mentioned line is executed, the control goes to the ApplicationDbContext class and executes BuildModel() method. I haven't done any changes to the AspNetRoles tables, but still the application is pointing me to run the migration scripts, Can anyone help me on fixing this issue? How do I add a role/claims dynamically to the Asp.Net identity tables in Asp.net5/EntityFramework7 ?

Note: in the RoleManager object during runtime i see the error message as "Value cannot be null.\r\nParameter name: entityType", please find attached snapshot


Solution

  • I found the solution for this problem. I need to add ApplicationRole also in the ApplicationDbContext class. This solves the problem.

     public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
        {
    ...
    ...
    }
    

    I got the solution from the post How do I extend IdentityRole using Web API 2 + AspNet Identity 2