5

我有一个具有多个.aspx页面的文件夹,我想限制其访问权限。我已使用<deny users="?"/>将web.config添加到该文件夹​​。如何在ASP.NET 2.0中使用UrlRewrite时优雅地处理ReturnUrl WebForms

问题是,在我使用UrlRewrite时,ReturnUrl是自动生成的.aspx文件的物理路径。

有没有办法操作ReturnUrl而不进行手动验证检查和重定向?有没有办法从代码隐藏或从web.config设置ReturnUrl?

编辑:该应用程序使用ASP.NET 2.0 WebForms。我不能使用3.5路由。

编辑2:似乎401状态码永远不会被捕获。它为受保护的页面返回302,并使用ReturnUrl重定向到登录页面。它不会为受保护的页面返回401。嗯...有趣的...参考:http://msdn.microsoft.com/en-us/library/aa480476.aspx

这使事情变得更加困难...我可能必须写反向重写映射规则,以正则表达式匹配ReturnUrl并将其替换,如果它不返回401 ...如果它返回401我可以将RawUrl设置为Response.RedirectLocation或将RawUrl替换为ReturnUrl。

任何人有任何其他想法?

+0

其他人有其他建议吗? – 2009-12-02 17:15:23

回答

1

我结束了检查在URL RETURNURL的存在和在Global.asax的EndRequest阶段用RawUrl替换它。这对我来说现在...

blog post帮助我设置它。

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

检查出来。希望这可以帮助。

#region [ Imports ] 

using System; 
using System.Web; 
using System.Web.Security; 

#endregion 

namespace Foo.Handlers 
{ 

    public class AuthModule : IHttpModule 
    { 

     #region IHttpModule Members 

     public void Init(HttpApplication application) 
     { 
      application.PostReleaseRequestState += delegate(object s, EventArgs e) 
       { 
        if (application.Response.StatusCode == 401) 
         application.Response.Redirect(FormsAuthentication.LoginUrl + "?ReturnUrl=" + HttpUtility.UrlEncode(application.Request.RawUrl), true); 
       }; 
     } 

     public void Dispose() { } 

     #endregion 

    } 

} 

<modules> 
    <add name="AuthModule" type="Foo.Handlers.AuthModule, Foo"/> 
</modules> 
+0

谢谢。我会试试这个。 – 2009-12-04 20:26:04

+0

没有机会在两者之间截取用ReturnUrl替换RawUrl。 – 2009-12-07 18:52:33

+0

状态码401永远不会被捕获......它会通过ReturnUrl(200)进入受保护页面(302) - >登录。 – 2009-12-07 19:15:11

1

创建以下控制适配器与改写的形式标记的action属性。我将这个ASP.NET 2.0应用程序与Intelligencia url重写器一起使用。我从这个blog post from the Gu得到它。

将这个类在App_Code文件夹:

using System.IO; 
using System.Web; 
using System.Web.UI; 

public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter 
{ 
    protected override void Render(HtmlTextWriter writer) 
    { 
     base.Render(new RewriteFormHtmlTextWriter(writer)); 
    } 
} 

public class RewriteFormHtmlTextWriter : HtmlTextWriter 
{ 
    public RewriteFormHtmlTextWriter(TextWriter writer) : base(writer) 
    { 
     base.InnerWriter = writer; 
    } 

    public RewriteFormHtmlTextWriter(HtmlTextWriter writer) : base(writer) 
    { 
     this.InnerWriter = writer.InnerWriter; 
    } 

    public override void WriteAttribute(string name, string value, bool fEncode) 
    { 

     // If the attribute we are writing is the "action" attribute, and we are not on a sub-control, 
     // then replace the value to write with the raw URL of the request - which ensures that we'll 
     // preserve the PathInfo value on postback scenarios 

     if ((name == "action")) 
     { 
      if (HttpContext.Current.Items["ActionAlreadyWritten"] == null) 
      { 

       // Because we are using the UrlRewriting.net HttpModule, we will use the 
       // Request.RawUrl property within ASP.NET to retrieve the origional URL 
       // before it was re-written. You'll want to change the line of code below 
       // if you use a different URL rewriting implementation. 
       value = HttpContext.Current.Request.RawUrl; 

       // Indicate that we've already rewritten the <form>'s action attribute to prevent 
       // us from rewriting a sub-control under the <form> control 
       HttpContext.Current.Items["ActionAlreadyWritten"] = true; 

      } 
     } 
     base.WriteAttribute(name, value, fEncode); 
    } 
} 

然后,在你App_Browsers文件夹中创建此.browser文件:

<browsers> 
    <browser refID="Default"> 
    <controlAdapters> 
     <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" /> 
    </controlAdapters> 
    </browser> 
</browsers> 
+0

似乎这个人在回传时会照顾网页的网址。我正在寻找的是当您未经身份验证的受限制页面时自动生成的ReturnUrl查询字符串的干净重写Url。 – 2009-12-04 20:10:29

+0

我相信它也应该这样做,但我不确定。 – craigmoliver 2009-12-04 20:30:44

相关问题