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