2012-10-14 89 views
2

如何拒絕直接訪問(通過輸入完整的url)到asp.net中的頁面,而不使用web.config中的角色。 我用:如何拒絕直接訪問頁面

if (Session.IsNewSession) 
      Response.Redirect("Default.aspx"); 

它是在第一時間一切的問題是確定和重定向工作,但如果我打開在同一瀏覽器的新選項卡,然後再次輸入URL失敗。 如何解決? 感謝名單

回答

3

試試這個:

Context.Items.Add("somevar","someval"); 

第2頁

if (Context.Items["somevar"] == null) 
{ 
    // the page is not redirected from Page 1 
} 
+0

我會用'Session'替代,因爲它會之間仍然存在要求。 Context.Items集合對於一個請求是有效的,所以如果OP需要一些登錄邏輯,那麼它不是一個好選擇。如果它是一個重定向頁面,那麼這是一個公平的解決方案。 – Patrick

0

使用會話就可以解決這個問題。

構建一個HttpModule並在context_BeginRequest中獲取當前的URL。稍後有條件地重定向到默認頁面。

public class RedirectionModule : IHttpModule 
{ 
    void context_BeginRequest(object sender, EventArgs e) 
    { 

     //this user already already eligible to go inside page ? 
     if (Session["eligible-to-go-inside"] == null) 
     { 
     //new user 
     //check current request url is default.aspx 
     //if not forward to default page 
     } 

    } 
} 

在Default.aspx頁面,如果用戶完全填補需求去內頁(如登錄),然後設置

Session["eligible-to-go-inside"] = "yes";