ASP.NET核心:调用视图组件不尊重授权属性
ASP.NET Core: calling a view component does not respect authorization attribute
我实现了 AspNetCore.Authorization 并用 [Authorize]
装饰了控制器 类。如果我从浏览器调用控制器,这会起作用。但是,视图组件可以访问数据:
查看:
@foreach (Category category in Model) {
<a class="btn btn-block
asp-action="Index"
asp-controller="Note"
asp-route-categoryid=@category.CategoryID
asp-route-page="1">
@category.Name
</a>
这导致视图组件被来自 @category.Name 的数据填充,实际上无法通过浏览器的地址栏访问。
视图组件:
[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
属性将拒绝来自视图组件的调用。我怎么会出现这种行为?
ViewComponents 不参与控制器生命周期,这意味着您不能在视图组件中使用过滤器。因此,Authorize
过滤器将不会执行。
View Component 必须使用 ViewComponent class 公开的 User 属性(获取当前用户的 System.Security.Principal.IPrincipal。)根据以下内容做出决定或更改输出用户 属性 的内容。
根据应用程序使用的身份验证类型,您需要验证用户的凭据。
我实现了 AspNetCore.Authorization 并用 [Authorize]
装饰了控制器 类。如果我从浏览器调用控制器,这会起作用。但是,视图组件可以访问数据:
查看:
@foreach (Category category in Model) {
<a class="btn btn-block
asp-action="Index"
asp-controller="Note"
asp-route-categoryid=@category.CategoryID
asp-route-page="1">
@category.Name
</a>
这导致视图组件被来自 @category.Name 的数据填充,实际上无法通过浏览器的地址栏访问。
视图组件:
[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
属性将拒绝来自视图组件的调用。我怎么会出现这种行为?
ViewComponents 不参与控制器生命周期,这意味着您不能在视图组件中使用过滤器。因此,Authorize
过滤器将不会执行。
View Component 必须使用 ViewComponent class 公开的 User 属性(获取当前用户的 System.Security.Principal.IPrincipal。)根据以下内容做出决定或更改输出用户 属性 的内容。 根据应用程序使用的身份验证类型,您需要验证用户的凭据。