0
我实现了AspNetCore.Authorization并用[Authorize]
修饰了控制器类。如果我从浏览器调用控制器,这将起作用。然而,视图组件可以访问数据:ASP.NET核心:调用视图组件不尊重授权属性
查看:
@foreach (Category category in Model) {
<a class="btn btn-block
asp-action="Index"
asp-controller="Note"
[email protected]
asp-route-page="1">
@category.Name
</a>
这导致视图组件从@ category.Name实际上不是由浏览器的地址栏访问充满了数据。
ViewComponent:
[Authorize]
public class NavigationMenuViewComponent : ViewComponent
{
private INoteRepository _Repository;
public NavigationMenuViewComponent(INoteRepository repository)
{
_Repository = repository;
}
public IViewComponentResult Invoke()
{
ViewBag.SelectedCategory = RouteData?.Values["category"];
return View(_Repository.Categories
.OrderBy(x => x.Name));
}
}
其实我想,Authorize
属性将拒绝来自如果没有实际登录的视图组件调用我要如何才能到达这样的行为?
谢谢!这让我走向了正确的方向!我用'IHttpContextAccessor context'去到HttpContext并分配'_User = context.HttpContext.User',这样我就可以检查'_User.Identity.IsAuthenticated == true'。 – pisker