title: assign multiple roles for a user in aspnet identity
body: <p>I have requirement to add multiple user roles for a particular user in user creation phase.</p>
  
  <p>So I'm going to achieve this using checkboxes in front end</p>
  
  <p>so this the viewpage</p>
  
  <pre><code>@model project_name.Models.RegisterViewModel
  
  @{
  
  }
  
  @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal"}))
  {
  
          &lt;div class="form-group"&gt;
              &lt;label class="col-md-2 control-label"&gt;
                  Select User Role
              &lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  @foreach (var item in (SelectList)ViewBag.RoleId)
                  {
                      &lt;input type="checkbox" name="SelectedRoles"
                             value="@item.Value" class="checkbox-inline" /&gt;
                      @Html.Label(item.Value, new { @class = "control-label" })
                  }
              &lt;/div&gt;
          &lt;/div&gt;
  
              ....
  
              &lt;div class="form-group"&gt;
                  &lt;div class="col-md-offset-2 col-md-10"&gt;
                      &lt;input type="submit" class="btn btn-default" value="Register" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  }
  </code></pre>
  
  <p>these are the changes I did over existing <code>AspNet Identity 2.0 framework</code> in <code>App_Start</code> folder => <code>IdentityConfig.cs</code> file</p>
  
  <pre><code>    public class RoleManager&lt;TRole&gt; : RoleManager&lt;TRole, string&gt; where TRole : class, IRole&lt;string&gt;
      {
          //
          // Summary:
          //     Constructor
          //
          // Parameters:
          //   store:
          public RoleManager(IRoleStore&lt;TRole, string&gt; store);
      }
  
         public class ApplicationRoleManager : RoleManager&lt;ApplicationRole&gt;
          {
              public ApplicationRoleManager(
                  IRoleStore&lt;ApplicationRole, string&gt; roleStore)
                  : base(roleStore)
              {
              }
              public static ApplicationRoleManager Create(
                  IdentityFactoryOptions&lt;ApplicationRoleManager&gt; options, IOwinContext context)
              {
                  return new ApplicationRoleManager(
                      new RoleStore&lt;ApplicationRole&gt;(context.Get&lt;ApplicationDbContext&gt;()));
              }
       }
  </code></pre>
  
  <p>these are the changes I did over existing <code>AspNet Identity 2.0 framework</code> in <code>Models</code> folder => <code>IdentityModels.cs</code> file    </p>
  
  <pre><code>    public class ApplicationUser : IdentityUser
  {
      public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(
          UserManager&lt;ApplicationUser&gt; manager)
      {
          var userIdentity = await manager.CreateIdentityAsync(
              this, DefaultAuthenticationTypes.ApplicationCookie);
          return userIdentity;
      }
  }
  
     public class ApplicationRole : IdentityRole
      {
          public ApplicationRole() : base() { }
          public ApplicationRole(string name) : base(name) { }
          public string Description { get; set; }
      }
  </code></pre>
  
  <p>So then I've set up controller's register method as follows</p>
  
  <pre><code>   // POST: /Account/Register
      [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public async Task&lt;ActionResult&gt; Register(RegisterViewModel model, HttpPostedFileBase upload, params string[] selectedRoles)
      {
  
          try
          {
              if (ModelState.IsValid)
              {
                  var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };                      
  
  
                  var result = await UserManager.CreateAsync(user, model.Password);
  
                  if (result.Succeeded)
                  {
                      //Add User to the selected Roles 
                      if (selectedRoles != null)
                      {
                          var addroles = await UserManager.AddToRolesAsync(user.Id, selectedRoles);
                          if (!addroles.Succeeded)
                          {
                              ModelState.AddModelError("", result.Errors.First());
                              ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync("Name", "Name"));
                              return View();
                          }
                      }
  
                  }
  
                  else
                  {
                      ModelState.AddModelError("", result.Errors.First());
                      ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                      return View();
                  }
  
                  return RedirectToAction("Index");
                  // AddErrors(result);
              }
  
          }
  
          // If we got this far, something failed, redisplay form
          catch (RetryLimitExceededException /* dex */)
          {
              //Log the error (uncomment dex variable name and add a line here to write a log.
              ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
          }
  
          ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
  
          return View(model);
      }
  </code></pre>
  
  <p>but each line of above controller that exist 'RoleManager' word I'm getting compile time error as below</p>
  
  <blockquote>
    <p>Using the generic type 'RoleManager TRole, TKey' requires 2 type
    arguments</p>
  </blockquote>
  
  <p>which means I'm getting 3 compile time errors in above method.</p>
  
  <p>Also I followed <a href="http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/" rel="nofollow">this reference</a> to get the idea to  assign multiple roles for a user in aspnet idenity</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I just changed <code>IdentityRole.cs[MetaData]</code> and <code>IdentityUserRole[MetaData]</code> also as follows</p>
  
  <pre><code>namespace Microsoft.AspNet.Identity.EntityFramework
  {
      //
      // Summary:
      //     Represents a Role entity
  
      public class IdentityRole : IdentityRole&lt;string, IdentityUserRole&gt;
      {
          //
          // Summary:
          //     Constructor
          public IdentityRole();
          //
          // Summary:
          //     Constructor
          //
          // Parameters:
          //   roleName:
          public IdentityRole(string roleName);
      }   
  
  public class IdentityRole&lt;TKey, TUserRole&gt; : IRole&lt;TKey&gt;
  where TUserRole : IdentityUserRole&lt;TKey&gt;
  {
      public TKey Id
      {
          get
          {
              return JustDecompileGenerated_get_Id();
          }
          set
          {
              JustDecompileGenerated_set_Id(value);
          }
      }
      public string Name
      {
          get;
          set;
      }
      public ICollection&lt;TUserRole&gt; Users
      {
          get
          {
              return JustDecompileGenerated_get_Users();
          }
          set
          {
              JustDecompileGenerated_set_Users(value);
          }
      }
      public IdentityRole()
      {
          this.Users = new List&lt;TUserRole&gt;();
      }
  }
  
  public class IdentityUserRole&lt;TKey&gt;
  {
      public virtual TKey RoleId
      {
          get;
          set;
      }
      public virtual TKey UserId
      {
          get;
          set;
      }
      public IdentityUserRole()
      {
      }
  }
  }
  </code></pre>