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;
}
我在Asp.Net Mvc 6中遇到的问题,GetRoles
和GetUserId
不存在,因为程序集的版本为Microsoft.AspNet.Identity.Core
,因此MVC 5中有两个问题。有人可以帮我纠正这个问题吗?
严重级代码描述项目文件行抑制状态 错误CS7036没有给出对应于'UserManager.UserManager(IUserStore,IOptions,IPasswordHasher,IEnumerable&gt;,IEnumerable&gt;,ILookupNormalizer,IdentityErrorDescriber,IServiceProvider,ILogger&gt;,IHttpContextAccessor)'LMS.DNX的所需形式参数'optionsAccessor'的参数4.5.1 D:\ Projects \ Library \ LMS \ src \ LMS \ Controllers \ RoleController.cs 50活动
// UsersController [授权] 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
[授权] 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");
}
}
我使用新方法修复了IsAdminUser,代码没有错误,我仍然没有好结果,如果有人知道如何修复它。请注意,谢谢。
答案 0 :(得分:1)
您可以使用以下代码检查用户是否处于角色中。
if(User.IsInRole("Admin"))
{
return true;
}
else
{
return false;
}