2009-03-06 62 views
0

我正在执行一个自定义身份验证方法,该方法使用轻型会话对象来保存用户的授权详细信息。现在我希望每个页面(主要是主页的子页面)能够分辨用户是否应该访问该页面。在ASP.NET中使用子页面的自定义授权

我应该创建一个页面类并从那里派生子页面吗?

应用程序知道哪些角色可以访问哪个页面的最佳方式是什么?

回答

0

如果您在授权方面需要很大的灵活性,您最好使用自定义页面类。否则,Web.config应该足够了。

0

如果你使用自定义角色提供程序插入它,实际上你可以依赖于asp.net配置。有一种方法可以让您检查用户是否有权访问给定页面:

System.Web.Security.UrlAuthorizationModule.CheckUrlAccessForPrincipal(
    "~/admin/test.aspx", principal, "GET" 
); 

然后,您可以在web.config上使用常规方法来配置授权。这样做时,如果页面位于同一文件夹中,则可以将web.config添加到该文件夹​​并适当配置授权。

0

在相同的情况下,我把需要的文件夹中的认证页面,我定义在web.config中的位置元素来配置这样的认证:

<location path="protected"> 
    <system.web> 
     <authorization> 
      <deny users="?"/> 
     </authorization> 
    </system.web> 
</location> 
1

我不喜欢的基本页面的方法。对我来说,检查安全措施已经太迟了。 您可以创建自己的HttpModule来检查,将授权信息存储在数据库/ xml/...中或使用页面上的反射来读取它。

context.Handler将持有正在执行的类Page。因此,你可以做这样的事情:

我复制我使用的代码的一部分,它会检查的作用,公共页面,跳过对图像和脚本(但你可以做得一样好)检查:

// In the HttpModule: 
    public void context_PreRequestHandlerExecute(object sender, EventArgs e) 
    { 
     HttpContext context = HttpContext.Current; 

     // Don´t validate permissions if the user wasn´t allowed by the asp.net security 
     // Neighter the advanced (custom) permissions are validated for non ASPX files. 
     if (!context.Request.FilePath.EndsWith(".aspx") || !context.User.Identity.IsAuthenticated) 
      return; 

     // Give full access to the unathorized error page, and logins, and so on... 
     string pageClass = context.Handler.GetType().BaseType.FullName; 

     string param = context.Request["p"]; 
     if (!string.IsNullOrEmpty(param)) 
      pageClass += "@" + param; 

     if (SecurityService.IsFullTrustClass(pageClass)) 
      return; 

     if (SecurityService.Context.CurrentPerson == null) 
     { 
      LogOff(); 
      return; 
     } 

     // Verify access permissions for the current page 
     IList<Role> roles = SecurityService.Context.CurrentPerson.Roles; 
     bool allow = SecurityService.HasAccessPermission(pageClass, roles); 

     if (!allow) 
     { 
      LogOff(); 
     } 
    }