2011-01-13 104 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方式使用表單身份驗證。