在 ASP.NET 2.0 WebForms 中使用 UrlRewrite 时如何优雅地处理 ReturnUrl
-
11-09-2019 - |
题
我有一个包含多个 .aspx 页面的文件夹,我想限制其访问。我已将 web.config 添加到该文件夹中 <deny users="?"/>
.
问题是,当我使用 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 或将 ReturnUrl 替换为 RawUrl。
还有其他人有其他想法吗?
解决方案 3
我最终检查了 Url 中是否存在 ReturnUrl,并在 Global.asax 的 EndRequest 阶段将其替换为 RawUrl。这目前对我有用......
这 博客文章 帮助我设置它。
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);
}
}
其他提示
检查出来。希望这有助于。
#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>
创建以下控制适配器与所述重写的形式为标签的action属性。余与 Intelligencia URL重写器结合使用这一个ASP.NET 2.0应用程序。我从这次的从谷的博客文章。
将这个类在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>
如果你正在使用ASP.NET 3.5,使用ASP.NET UrlRouting代替。但是,你必须检查授权manualy。
HTTP://chriscavanagh.wordpress。 COM / 2008/03/11 / ASPNET路由-再见-URL重写/