Reputation: 49
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?
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.
Upvotes: 0
Views: 1847
Reputation: 31
You can check if the user is in role with following code.
if(User.IsInRole("Admin"))
{
return true;
}
else
{
return false;
}
Upvotes: 2