【问题标题】:public Boolean isAdmin in Asp.Net Mvc 6Asp.Net Mvc 6 中的公共布尔 isAdmin
【发布时间】:2016-07-03 04:44:10
【问题描述】:
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>, IHttpContextAccessor)” LMS.DNX 的所需形式参数“optionsAccessor”相对应的参数4.5.1 D:\Projects\Library\LMS\src\LMS\Controllers\RoleController.cs 50 活动

// 用户控制器 [授权] 公共类用户控制器:控制器 { 私有只读 UserManager _userManager; 私有 ApplicationDbContext _context; 公共用户控制器( 用户管理器用户管理器 ) { _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; 私有 ApplicationDbContext _context; 公共角色控制器( 用户管理器用户管理器 ) { _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,代码没有错误,我仍然没有好的结果,如果有人知道如何修复它。记下感谢。

【问题讨论】:

  • 升级你的asp身份版本
  • 我认为 UserManager 有UserManager.GetRolesAsync()
  • 我已经尝试过 UserManager.GetRolesAsync(user.Name, "Admin"),但它不起作用。此代码在 UserController.cs 中。

标签: c# asp.net-core-mvc


【解决方案1】:

您可以使用以下代码检查用户是否在角色中。

if(User.IsInRole("Admin"))
{
    return true;
}
else
{
    return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多