ASP.NET MVC 3中的自定义授权

use*_*662 6 c# asp.net-mvc asp.net-mvc-3

我正在将一个应用程序从ASP.NET Web Forms迁移到ASP.NET MVC 3.其中一个核心和关键部分目前已锁定在自己的目录中.我通过在web.config文件中使用以下内容来限制未经授权的用户访问此目录:

<location path="home" allowOverride="false">
  <system.web>
    <authorization>
      <deny users="?"/>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何在ASP.NET MVC 3中实现这种相同类型的安全性?我有一种预感,它涉及在我的Controller类上设置属性.但是,AuthorizeAttribute看起来只接受用户名列表而不是auth状态(如果我错了,请纠正我).我查看了示例ASP.NET Internet应用程序,但没有看到任何特殊的配置.

有人可以指点我正确的方向吗?

谢谢!

小智 5

这是正确的,你会利用AuthorizeAttribute,如下:

 [Authorize]
 public ActionResult AuthenticatedUsers()
 {
     return View();
 }

 [Authorize(Roles = "Role1, Role2")]
 public ActionResult SomeRoles()
 {
     return View();
 }

 [Authorize(Users = "User1, User2")]
 public ActionResult SomeUsers()
 {
     return View();
 }
Run Code Online (Sandbox Code Playgroud)

至于"身份证身份",我不确定我知道你的意思.这听起来像Roles处理该身份验证要求.