0
public Boolean isAdminUser()    
{    
    if (User.Identity.IsAuthenticated)    
    {    
        var user = User.Identity;    
        ApplicationDbContext context = new ApplicationDbContext();    
        var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));    
        var s = UserManager.GetRoles(user.GetUserId());    
        if (s[0].ToString() == "Admin")    
        {    
            return true;    
        }    
        else    
        {    
            return false;    
        }    
    }    
    return false;    
}    

My problem in Asp.Net Mvc 6, GetRoles and GetUserId does not exist, in MVC 5 have two of these, due to the version of assembly: Microsoft.AspNet.Identity.Core. Can someone help me to correct this problem?

As mentioned above code, I execute I encounter as follows:

Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'optionsAccessor' of 'UserManager.UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, ILogger>, IHttpContextAccessor)' LMS.DNX 4.5.1 D:\Projects\Library\LMS\src\LMS\Controllers\RoleController.cs 50 Active

// UsersController [Authorize] public class UsersController : Controller { private readonly UserManager _userManager; private ApplicationDbContext _context; public UsersController( UserManager userManager ) { _userManager = userManager; _context = new ApplicationDbContext();

    }
    // GET: /Role/Users
    public async Task<IActionResult> IsAdminUser(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            if (User.Identity.IsAuthenticated)
            {
                var userId = User.GetUserId();
                ApplicationUser user = await _userManager.FindByIdAsync(userId);
                if (!IsAdminUser())
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            return RedirectToAction("Index", "Home");
        }
        return View(model);
    }

    private bool IsAdminUser()
    {
        if (User.Identity.IsAuthenticated)
        {
            var userId = User.GetUserId();
            var user = _userManager.FindByIdAsync(userId);
            if (user.ToString() == "Admin")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }

    // GET: /<controller>/
    // method is async and returns a Task
    public async Task<IActionResult> Index()
    {
        if (User.Identity.IsAuthenticated)
        {
            var user = User.Identity;
            ViewBag.Name = user.Name;
            //  ApplicationDbContext context = new ApplicationDbContext();
            //  var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

            //var s=    UserManager.GetRoles(user.GetUserId());
            ViewBag.displayMenu = "No";

            if (IsAdminUser())
            {
                ViewBag.displayMenu = "Yes";
            }
            return View();
        }
        else
        {
            ViewBag.Name = "Not Logged IN";
        }


        return View();
    }
}

// RoleController

[Authorize] public class RoleController : Controller { private readonly UserManager _userManager; private ApplicationDbContext _context; public RoleController( UserManager userManager ) { _userManager = userManager; _context = new ApplicationDbContext();

    }

    // GET: /<controller>/
    public async Task<IActionResult> Index()
    {
        if (User.Identity.IsAuthenticated)
        {


            if (!IsAdminUser())
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }

        var Roles = _context.Roles.ToList();
        return View(Roles);
    }

    // GET: /Role/Users
    public async Task<IActionResult> IsAdminUser(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            if (User.Identity.IsAuthenticated)
            {
                var userId = User.GetUserId();
                ApplicationUser user = await _userManager.FindByIdAsync(userId);
                if (!IsAdminUser())
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            return RedirectToAction("Index", "Home");
        }
        return View(model);
    }

    private bool IsAdminUser()
    {
        if (User.Identity.IsAuthenticated)
        {
            var userId = User.GetUserId();
            var user = _userManager.FindByIdAsync(userId);
            if (user.ToString() == "Admin")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }

    public IActionResult Create()
    {
        if (User.Identity.IsAuthenticated)
        {


            if (!IsAdminUser())
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }

        var Role = new IdentityRole();
        return View(Role);
    }
    [HttpPost]
    public IActionResult Create(IdentityRole role)
    {
        if (User.Identity.IsAuthenticated)
        {
            if (!IsAdminUser())
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }

        _context.Roles.Add(role);
        _context.SaveChanges();
        return RedirectToAction("Index");
    }
}

I fix the IsAdminUser with the new method, code has no error, I still don't have good result, if anybody know how to fix it. Note with thank.

4
  • Does this help? stackoverflow.com/a/30701830/1663001 Commented Mar 17, 2016 at 2:00
  • upgrade your asp identity version Commented Mar 17, 2016 at 2:03
  • I think UserManager have UserManager.GetRolesAsync() Commented Mar 17, 2016 at 2:08
  • I have tried UserManager.GetRolesAsync(user.Name, "Admin"), but it does not work. This code is in UserController.cs. Commented Mar 17, 2016 at 2:16

1 Answer 1

2

You can check if the user is in role with following code.

if(User.IsInRole("Admin"))
{
    return true;
}
else
{
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.