Search code examples
c#asp.netentity-frameworkasp.net-identity

ASP.NET Identity: Additional properties to ApplicationUserRole


I have added an additional property to ApplicationUserRole as follows:

public class ApplicationUserRole : IdentityUserRole<int>
    {
        public string RoleAssigner { get; set; }
    }

Now i am to assign a role to an user as follows:

[HttpPost]
    public ActionResult Create(UserRoleViewModel userRoleViewModel)
    {
        if (ModelState.IsValid)
        {
            using (var context = new ApplicationDbContext())
            {
                var userRole = new ApplicationUserRole
                {
                    UserId = userRoleViewModel.UserId,
                    RoleId = userRoleViewModel.RoleId,
                    RoleAssigner = userRoleViewModel.RoleAssigner
                };
                context.ApplicationUserRoles.Add(userRole);
                context.SaveChanges();
                return RedirectToAction("Index");
            }               
        }
        return View(userRoleViewModel);
    }

This is working fine!!

Before adding additional "RoleAssigner" Property, I can assign a role to an user using AddToRoles() Method as follows:

[HttpPost]
    public ActionResult Create(UserRoleViewModel userRoleViewModel)
    {
        if (ModelState.IsValid)
        {      
             UserManager.AddToRoles(userRoleViewModel.Id,   userRoleViewModel.RoleName);
            return RedirectToAction("Index");
         }

        return View(userRoleViewModel);
    }

My question is: After adding an additional Property like "RoleAssigner", Is there any way to assign a role to an user using AddToRoles() method which will also insert the additional "RoleAssigner" value for "RoleAssigner" column in the database also.


Solution

  • Edit with working example:

    I think you can do that by creating an extension method at IdentityConfig. I did something similar to find user by username or phone number

    In what I'm able to understand you want to call UserManager.AddToRoles(...) and fill the new role property.

    to do that( in similar to the example before ) you need an extension to user manager. you do it like this:

    public static class UserManagerExtens
    {
        public static IdentityResult AddToRole(this ApplicationUserManager userManager,string userId,string[] roles,string assigner)
        {
            try
            {
                ApplicationUserRole role = null;
                using (ApplicationDbContext context = new ApplicationDbContext())
                {
                    foreach (var item in roles)
                    {
                        role = new ApplicationUserRole();
                        role.UserId = userId;
                        role.RoleAssigner = assigner;
                        role.RoleId = item;
                        context.AspNetUserRoles.Add(role);
                    }
                    context.SaveChanges();
                }
                return new IdentityResult() { };
            }
            catch (Exception ex)
            {
                return new IdentityResult(ex.Message);
            }
        }
    }
    

    That is a working example, using UserManager you can call it with definded parameters like:

    string[] roles = new string[] { /*your roles here*/ };
    UserManager.AddToRole(/*UserIdHere*/, roles, /*assigerId here*/);
    

    Similar to this you can implement async or other methods of UserManager.