Mar*_*cel 4 c# asp.net-mvc asp.net-mvc-5
我的控制器有很多动作。某些用户只能访问所有内容,但以下一项操作除外:
[Authorize(Roles = "Admin")]
public class SecretsController : Controller
{
[Authorize]
public ActionResult Index()
{
return View(...);
}
...
}
Run Code Online (Sandbox Code Playgroud)
甚至[Authorize (Roles = null)]不起作用。方法属性将被忽略!我如何仅通过一项操作就能获得例外?喜欢AllowAnonymous允许它,但对登录用户可见吗?
您可以OverrideAuthorization像下面的代码一样使用属性:
[Authorize(Roles = "Admin")]
public class SecretsController : Controller
{
[OverrideAuthorization]
[Authorize()]
public ActionResult Index()
{
return View(...);
}
...
}
Run Code Online (Sandbox Code Playgroud)
随[OverrideAuthorization]ASP.Net MVC5附带的组件,您告诉您的Index 操作重写/忽略在控制器级别定义的授权规则。
这样,您定义的所有操作SecretsController仅对Admin角色可见,除了Index仅对经过身份验证的用户可见的操作,即使这些操作不具有Admin角色也是如此。
| 归档时间: |
|
| 查看次数: |
879 次 |
| 最近记录: |