2012-03-30 40 views
0

我正在使用自己的數據庫表的ASP.NET成員資格的自定義實現。一切正常,但我需要將尚未支付發票的客戶重定向到付款頁面。這不僅會在登錄時發生,還會發生在已經登錄的用戶身上,因此如果發票在用戶登錄時被註冊爲「未付款」,則用戶必須重定向到付款頁面,下次他們加載一個頁面。ASP.NET成員資格 - 重定向用戶未支付發票

可以這樣做嗎?

回答

2

我使用HttpModule做了類似於此的操作。你想要做的是處理PreRequest事件,如果他們已經過身份驗證,並且如果是這樣,使您的未付帳單檢查並根據需要重定向。

例如

protected void OnPreRequestHandlerExecute(object sender, EventArgs e) 
{ 
    if (HttpContext.Current.Request.Path != "/UnPaid.aspx" && HttpContext.Current.User != null) 
     { 
      if (HttpContext.Current.User.Identity.IsAuthenticated) 
      { 
       // You don't want to make this check for every resource type 
       bool isPage = HttpContext.Current.Request.Path.EndsWith(".aspx") || HttpContext.Current.Request.Path == "/"; 
       if (isPage) 
       { 
        bool isPaid = false; // Make isPaid check here 
        if (!isPaid) 
        { 
         // Optional pass ina return url for after the invoice is paid 
         string returnUrl = HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl, HttpContext.Current.Request.ContentEncoding); 

         HttpContext.Current.Response.Redirect(string.Concat("/UnPaid.aspx?ReturnUrl=", returnUrl), true); 
        } 
       } 
      } 
     } 
    } 
2

我不會讓會員供應商知道這些信息。知道這一點是你的應用程序的工作,而不是你的安全模型。它可能就像添加/刪除角色一樣簡單,但這也不是理想的。

2

您可以使用Application_AuthenticateRequest

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
     string cTheFile = HttpContext.Current.Request.Path; 

     if(!cTheFile.EndsWith("ThePaymentPage.aspx")) 
     {   
      if(HttpContext.Current.User != null 
      && HttpContext.Current.User.Identity != null 
       && HttpContext.Current.User.Identity.IsAuthenticated) 
      { 
       // check here if need to redirect or not. 
       if(NeedPayment()) 
       { 
       HttpContext.Current.Responce.Redirect("ThePaymentPage.aspx"); 
       } 
      } 
     } 
} 

這就是所謂的每一頁上都做對global.asax,所以也許你可以添加一些更多的檢查,使之真正快。如果頁面結束,其他檢查可以是.aspx

0

如何繼承。 您可以在Page類和您的ASPX代碼之後「注入」一個BasePage。 通過這種方式,您將可以訪問業務邏輯類,然後可以根據每個請求確定用戶應該重定向到的位置。

我也同意這個邏輯應該由您的應用程序業務邏輯而不是安全模型來處理。

希望這會有所幫助。