ASP.NET成员资格 - 使用未付发票重定向用户

时间:2012-03-30 11:58:08

标签: asp.net asp.net-membership

我正在开发ASP.NET成员资格的自定义实现,它使用我自己的数据库表。一切都按预期工作,但我需要将尚未支付发票的客户重定向到付款页面。这不仅应该在登录时发生,而且对于已经登录的用户也应该发生,因此如果在用户登录时发票被注册为“未付款”,则必须将用户重定向到付款页面,下次他们加载页面。

可以这样做吗?

4 个答案:

答案 0 :(得分:2)

我不会让会员提供者知道这些信息。了解这一点是您的应用程序的工作,而不是您的安全模型。 可能就像添加/删除角色一样简单,但这也不理想。

答案 1 :(得分:2)

您可以使用global.asax

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");
              }
          }
        }
}

每个页面都会调用它,因此您可以添加更多检查并使其快速实现。如果页面在.aspx

结束,则可以进行其他检查

答案 2 :(得分:2)

我使用HttpModule做了类似的事情。您要做的是处理PreRequest事件以及是否经过身份验证,如果是,请根据需要检查并重定向您的未付款发票。

e.g。

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);
                    }
                }
            }
        }
    }

答案 3 :(得分:0)

继承怎么样? 您可以在Page类和后面的ASPX代码之间“注入”一个BasePage。 这样,您就可以访问业务逻辑类,然后就每个请求决定用户应该重定向的位置。

我也同意这个逻辑应由应用程序业务逻辑而不是安全模型处理。

希望这有帮助。