ASP.NET MVC2 拒绝使用 IIS 7 访问控制器?

发布于 2024-10-08 17:54:21 字数 113 浏览 0 评论 0原文

我有一个名为 admin 的目录。 该目录没有物理路径,因为它被硬编码为我的管理区域的前缀。

如何通过标准认证来保护这条路径? 我希望只有管理员用户才能登录该区域。 但主页需要对每个用户都可用。

I have a directory that is named admin.
There is no physical path to this directory, as it is hard-coded as prefix for my administration area.

How can I protect this path with standard-authentification?
I want that only user Admin can login to that area.
But the main page needs to be available for every user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-10-15 17:54:21

如果您使用任何内置身份验证变体:

[Authorize(Roles = "Administrator")]
public class AdminController : Controller
{
}

编辑

要限制为特定用户:

[Authorize(Users = "Admin")]
public class AdminController : Controller
{
}

编辑2

是的,您可以,但这有点黑客攻击。创建您自己的类并从 RouteBase 派生它。

public class MyRoute : RouteBase
{

     public override RouteData GetRouteData(HttpContextBase context)
     {
          if (context.Request.Uri == XXX && context.User != YYYY)
              return forbiddenRoute;//redirect to forbidden page.
          else
              return null;
     }
}

然后将其添加到您的 global.asax 中。

If you are using any of the built in authentication variants:

[Authorize(Roles = "Administrator")]
public class AdminController : Controller
{
}

Edit

To limit to a specific user:

[Authorize(Users = "Admin")]
public class AdminController : Controller
{
}

Edit2

Yes you can, but it's kind of a hack. Create your own class and derive it from RouteBase.

public class MyRoute : RouteBase
{

     public override RouteData GetRouteData(HttpContextBase context)
     {
          if (context.Request.Uri == XXX && context.User != YYYY)
              return forbiddenRoute;//redirect to forbidden page.
          else
              return null;
     }
}

Then add it in your global.asax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文