Search code examples
c#asp.netcontrollerasp.net-core-mvcrole-manager

MVC5 to MVC6 converstion can't get RoleManager to work


I am trying to upgrade an MVC 5 application to MVC 6. The only thing in the 5 application is the ability to administrate Users and Roles. It is a "template" we created at work to wrap around all MVC projects to give them all the "login, register, and roles" stuff you need for security. It works well. However, we need MVC 6 version too.

We were able to get MVC 6 installed with single user authentication and now I am trying to port over the Roles process from the working MVC 5 application.

My RolesManagementController.cs works in 5, but in 6 I get a red line under "RoleManager(IdentityRole)"

enter image description here

Also, red lines under ".RoleExists(" and ".Update"). enter image description here

Here are my using statements in 6 version:

using System;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
using MVC6.Models;

and my using statements in 5 version are not that much different.

using System;
using System.Linq;
using System.Web.Mvc;
using Donut5.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

Solution

  • With the new version of Asp.net (Asp.net 5) those methods became async. you should make your method async and return Task<T> or just Task and call role manager methods with the await keyword

    await _rolesManager.RoleExistsAsync(...)
    await _rolesManager.UpdateAsync(...)
    

    Example:

    public async Task MethodName()
    {
        var role = await _rolesManager.RoleExistsAsync(...);
        await _rolesManager.UpdateAsync(...);
    }