บูลีนสาธารณะ isAdmin ใน Asp.Net Mvc 6

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 ไม่มีอยู่ ใน MVC 5 มีสองสิ่งนี้เนื่องจากเวอร์ชันของแอสเซมบลี: Microsoft.AspNet.Identity.Core ใครสามารถช่วยฉันแก้ไขปัญหานี้ได้บ้าง

ดังที่ได้กล่าวไว้ข้างต้นโค้ด ฉันรันฉันพบดังนี้:

รหัสความรุนแรงคำอธิบายข้อผิดพลาดสถานะการระงับบรรทัดไฟล์โครงการ CS7036 ไม่มีอาร์กิวเมนต์ที่กำหนดซึ่งสอดคล้องกับพารามิเตอร์อย่างเป็นทางการที่จำเป็น 'optionsAccessor' ของ '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 ใช้งานอยู่

// UsersController [อนุญาต] UsersController ระดับสาธารณะ: ตัวควบคุม { UserManager แบบอ่านอย่างเดียวส่วนตัว _userManager; ApplicationDbContext ส่วนตัว _context; UsersController สาธารณะ ( UserManager userManager ) { _userManager = userManager; _context = 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

[อนุญาต] RoleController ระดับสาธารณะ: ตัวควบคุม { UserManager แบบอ่านอย่างเดียวส่วนตัว _userManager; ApplicationDbContext ส่วนตัว _context; บทบาทสาธารณะ ( UserManager userManager ) { _userManager = userManager; _context = 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 ด้วยวิธีการใหม่ โค้ดไม่มี error ก็ยังได้ผลไม่ดีนัก ถ้าใครทราบวิธีแก้ไขครับ รับทราบด้วยความขอบคุณ.


person Sisovin    schedule 17.03.2016    source แหล่งที่มา
comment
สิ่งนี้ช่วยได้หรือไม่? stackoverflow.com/a/30701830/1663001   -  person DavidG    schedule 17.03.2016
comment
อัปเกรดเวอร์ชันข้อมูลประจำตัว asp ของคุณ   -  person DarkMakukudo    schedule 17.03.2016
comment
ฉันคิดว่า UserManager มี UserManager.GetRolesAsync()   -  person Kien Chu    schedule 17.03.2016
comment
ฉันได้ลองใช้ UserManager.GetRolesAsync(user.Name, Admin) แล้ว แต่มันไม่ทำงาน รหัสนี้อยู่ใน UserController.cs   -  person Sisovin    schedule 17.03.2016


คำตอบ (1)


คุณสามารถตรวจสอบว่าผู้ใช้มีบทบาทหรือไม่ด้วยรหัสต่อไปนี้

if(User.IsInRole("Admin"))
{
    return true;
}
else
{
    return false;
}
person Manja    schedule 18.03.2016