【问题标题】:Protect some pages from direct access in ASP.NET保护某些页面不被 ASP.NET 直接访问
【发布时间】:2010-05-24 23:05:37
【问题描述】:

我有一个名为 admin.aspx 的 ASP.NET 页面,需要保护它不被直接访问。

我希望只有当用户在另一个名为 login.aspx 的页面中输入他的姓名和密码时才能访问它。

我正在使用 Visual Basic .NET 2008 在 ASP.NET 中工作,但我不知道该怎么做。

我该怎么做?

【问题讨论】:

  • 请定义“直接访问”。您是指通过 HTTP 的用户请求吗?还是在文件系统中打开文件?

标签: asp.net vb.net


【解决方案1】:

此行为的正确术语是Authorization

我需要事先知道的一些事情:

  • 您有自己的登录/注销逻辑吗?
  • 您使用的是自定义用户数据库/表吗?
  • 如果以上两个都用yes 回答:您是否阅读/听说过有关Membership- 和RoleProviders 的信息?

.NET 有很好的内置机制来解决这个问题。它不仅提供了很好的配置可能性,而且实现起来也非常简单

下面是关于 ASP.NET Membership Provider 的非常详细的介绍:

ASP.NET 2.0 Membership and Roles Tutorial Series

尽管它使用的是 ASP.NET 2.0 和 C#,但在 .NET3.5/4.0 和 VB 上应该没有什么不同。网络

【讨论】:

  • - Part 2 - master how to create roles and assign users to roles. This article shows how to setup roles, using role-based authorization, and displaying output on a page depending upon the visitor's roles. 您只需要阅读第 2 部分即可。这不会花费太长时间 ;-)。
【解决方案2】:

我找到了:

在登录页面(“login.aspx”)中这样做:

Session("Name") = "Yes"

Response.Redirect("admin.aspx")

在管理页面(“admin.aspx”)中:

If Session("Name") = "Yes" Then
    'You can here display anything you want, or just leave it blank
Else
    Response.Redirect("ErrorPage.aspx")
End If

【讨论】:

    【解决方案3】:

    您应该在加载页面之前先检查用户会话:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (session == null)
        {
            // Just redirect to login page or no access page warning.**
        }
    
        if (!Page.IsPostBack)
        {
           //If your were logged in then you will access this page
        }
    }
    

    【讨论】:

      【解决方案4】:

      您可以通过Forms authentication 处理它。在您的情况下,您希望确保限制对 admin.aspx 的访问,因此您可以通过指定位置标记在 web .config 中提供该条目来实现。看看这个网站:

      http://www.dnzone.com/go?60

      HTH

      【讨论】:

      • Raja(保护了这个页面后,我怎么能访问它们而其他人不能呢?)
      • 正如您所说,您必须提供用户名和密码(登录页面),以验证您访问该页面的身份。
      猜你喜欢
      • 2017-05-29
      • 1970-01-01
      • 2016-02-23
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多