我知道可以用 Authorize 属性装饰 Controller 来控制访问,但我不知道在区域中的所有 Controller / View 之间强制执行安全性的可接受或正确的方法。
web.config、区域注册或其他地方是否有应用授权安全性的内容?
最佳答案
一个方便的方法是创建一个新的基类
[Authorize]
public abstract class AuthorizeBaseController : Controller
{
}
并确保您需要授权的所有 Controller (在您的情况下,您关心的区域中的所有 Controller )都来自 AuthorizeBaseController
.
public class HomeController : AuthorizeBaseController
{
public ActionResult Index()
{
return View();
}
}
[Authorize]
属性应该影响新基类的所有后代。
编辑我在使用 <location path="" >
时遇到的问题方法是,由于路由引擎使任何路由都可以调用任何 Controller ,因此根据 url(以及特定路由)而不是 Controller 操作设置授权,可以调用应 protected 并跳过的 Controller 授权。这在 Web 表单中不是问题,因为页面就是页面(而不是方法调用),但 MVC 中页面/路径和代码之间的分离使得这成为一个巨大的安全漏洞。
关于asp.net-mvc - asp.net mvc 的区域级安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597446/