How to elegantly handle ReturnUrl when using UrlRewrite in ASP.NET 2.0 WebForms

前端 未结 4 1758
情歌与酒
情歌与酒 2021-01-13 03:12

I have a folder with multiple .aspx pages that I want to restrict access to. I have added web.config to that folder with .

The

4条回答
  •  盖世英雄少女心
    2021-01-13 03:38

    I ended up checking for existence of ReturnUrl in the Url and replacing it with RawUrl in EndRequest stage in Global.asax. This works for me for now...

    This blog post helped me setting it up.

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        string redirectUrl = this.Response.RedirectLocation;
        if (!this.Request.RawUrl.Contains("ReturnUrl=") && !string.IsNullOrEmpty(redirectUrl))
        {
            this.Response.RedirectLocation = Regex.Replace(redirectUrl, "ReturnUrl=(?'url'[^&]*)", delegate(Match m)
            {
                return string.Format("ReturnUrl={0}", HttpUtility.UrlEncode(this.Request.RawUrl));
            }, RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
        }
    }
    

提交回复
热议问题