2
我有一个MVC 6网站(asp.net核心/ 5),我需要检查用户是否在特定角色或不是从剃刀页面。每当我拨打User.IsInRole("{rolename}")
时,它每次都会返回false。如果我只是打电话给我的RoleRepository,它就可以工作。在ASP.NET核心覆盖User.IsInRole
是否需要在某个位置重写User.IsInRole()
功能,还是该方法在ASP.NET Core中已过时?
这里是我的执行至今:
if (User.IsInRole("Admin") || User.IsInRole("SuperUser") || User.IsInRole("DataIntegrity"))
{
model.IsDataIntegrity = true;
}
这正常工作,当我通过授权政策调用它,就像这样:
[Authorize("DataIntegrity")]
[ValidateAntiForgeryToken]
[HttpPost]
[Route("team/edit/{schoolId:int:min(1)}/{schoolName?}")]
public async Task<IActionResult> Edit(SchoolEditViewModel model)
{
model.SchoolInfo = await _schoolRepo.GetSchoolInfo(model.SchoolId, _currentSeason);
IsModelValid(model);
if (!ModelState.IsValid)
{
return View(await ApplyUnboundProperties(model));
}
await SaveSchool(model);
return RedirectToAction("Live", "SchoolMain", new {schoolId = model.SchoolId, schoolName = model.SchoolInfo.SchoolName});
}
不了'IPrincipal'的东西还有吗?你可以写一个扩展方法来达到同样的效果。该属性是否仍然存在于核心中? –
下面是一个来自github上aspnet源代码的例子:https://github.com/aspnet/Mvc/blob/dev/test/WebSites/FiltersWebSite/Filters/AuthorizeUserAttribute.cs –
看起来你现在拥有'IAuthorizationFilter'东西。 –