2011-12-28 73 views
1

我的大部分ASP.NET網站都可供匿名網絡用戶訪問。不過,在允許訪問之前,我需要驗證幾個頁面。我通過在web.config控制這樣的:在登錄頁面上添加自定義內容

<authorization> 
    <allow users="*"/> 
</authorization> 

目前我logon.aspx文件是通用的,但我真的想告訴包括他爲什麼被重定向到登錄頁面的用戶指令。例如:

在您可以自願執行任務之前,請先登錄,系統才能識別您的身份。

OR

你已經嘗試編輯的事件。該系統只允許管理員執行此操作。請登錄,以便我們確認您是此活動的管理員。

登錄頁面上的說明取決於用戶在表單認證重定向之前嘗試的內容。

我的問題是,登錄頁面如何確定採取了哪些操作?有沒有辦法將自定義查詢字符串傳遞到登錄頁面?我想我可以解碼ReturnUrl並使用它來嘗試確定要顯示的指令。然而,這種方法只是覺得......髒。我不喜歡登錄頁面依賴於系統中其他頁面的URL名稱。

有什麼建議嗎?

回答

0

爲什麼不使用自定義例外來做到這一點。

public class ClassName: Exception 
    { 
     protected ClassName() : base() {} 
     public ClassName(String message) : base(message) { } 
    } 

然後你做

public class First: ClassName 
{ 
    public First() : base("message") {} 
} 

public class Second: ClassName 
{ 
    public Second() : base("message") { } 
} 

現在你只趕上型類名的例外,看到消息的值,並將其傳遞到ASP標籤或文本框或根據需要做

+0

如何處理異常pro穿越HTTP重定向? – Aheho 2011-12-28 17:26:57

0

您也可以在global.asax的Application.EndRequest中捕獲重定向。

void Application_EndRequest(object sender, EventArgs e) 
    { 
     if (!HttpContext.Current.Request.IsAuthenticated && HttpContext.Current.Response.StatusCode == 302 && HttpContext.Current.Response.RedirectLocation != null && HttpContext.Current.Response.RedirectLocation.ToLower().Contains("login.aspx")) 
     { 
      // do some switching or determining here, or have items preset in your HttpContext.Current.Items[] 
      HttpContext.Current.Response.RedirectLocation += "&Action=blah"; 

     } 
    }