在Asp.Net Mvc 6中,public Boolean isAdmin

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中, GetRolesGetUserId不存在,在MVC 5中有两个这样的,由于汇编版本: Microsoft.AspNet.Identity.Core 。 有人可以帮我纠正这个问题吗?

正如上面提到的代码,我执行我遇到如下:

严重级代码说明项目文件行抑制状态错误CS7036没有给出的参数对应于'UserManager.UserManager(IUserStore,IOptions,IPasswordHasher,IEnumerable>,IEnumerable>,ILookupNormalizer,IdentityErrorDescriber,IServiceProvider,ILogger所需的形式参数'optionsAccessor' >,IHttpContextAccessor)'LMS.DNX 4.5.1 D: Projects Library LMS src LMS Controllers RoleController.cs 50活动

// 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:控制器{私人只读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,代码没有错误,如果有人知道如何修复它,我仍然没有很好的结果。 请注意谢谢。


您可以检查用户是否具有以下代码的角色。

if(User.IsInRole("Admin"))
{
    return true;
}
else
{
    return false;
}
链接地址: http://www.djcxy.com/p/82097.html

上一篇: public Boolean isAdmin in Asp.Net Mvc 6

下一篇: Simple Dependency Resolver