2011-01-13 110 views
0

我正在开发一个ASP.NET网站。我坐在一些页面上,我希望他只有在注册用户的情况下才能看到。如果没有,他应该被重定向到适当的页面。我是否应该在用户登录的Page_load事件中检查此事件?防止用户访问某些页面直到注册

如果用户键入URL的名称,这仍然可以工作,对吧?

回答

0

这是我为一些最简单的网站做的。它使用典型的forms authentication。从global.ascx.cs:

protected void Application_PostAuthenticateRequest(object sender, EventArgs e) 
{ 
    var unsecuredPages = new[] 
          { 
           "test.htm", 
           "email.htm" 
          }; 

    bool letThrough = unsecuredPages.Any(s => Request.RawUrl.ToLowerInvariant().Contains(s)); 
    if (letThrough) 
    { 
     return; 
    } 

    if (!User.Identity.IsAuthenticated && 
     !Request.RawUrl.Contains(FormsAuthentication.LoginUrl)) 
    { 
     FormsAuthentication.SignOut(); 
     FormsAuthentication.RedirectToLoginPage(); 
    } 
} 

您明确指定要显示给所有用户和所有其他请求都将被重定向到登录页面的网页。我的web.config文件看起来是这样的:

<authentication mode="Forms"> 
    <forms loginUrl="Login.aspx" protection="All" timeout="480" name="xyz" path="/" requireSSL="false" slidingExpiration="true" defaultUrl="Default.aspx" cookieless="UseDeviceProfile" enableCrossAppRedirects="true"/> 
</authentication> 
<location path="Styles"> 
    <system.web> 
     <authorization> 
      <allow users="*"/> 
     </authorization> 
    </system.web> 
</location> 
+0

是否有一个原因,你不只是添加元素为这两个不安全的网页,就像你有你的样式文件夹? – patmortech 2011-01-13 05:06:22

0

是的,你可以使一个类,并在该类可以检查用户被登记,并继承上所需要的注册用户的网页该类。

你也可以使用上面的@HeavyWave方式使用表单身份验证。