我不知道这是否正是您想要的,但总的来说,您应该能够通过使用一些反射来欺骗UrlReferer
属性的值(即使它是只读的) 。HttpContext.Current.Request
例如:
FieldInfo fi = HttpContext.Current.Request.GetType().GetField("_referrer", BindingFlags.NonPublic | BindingFlags.Instance);
string initialReferer = HttpContext.Current.Request.UrlReferrer.ToString();
if (fi != null)
fi.SetValue(HttpContext.Current.Request, new Uri("http://example.com"));
string fakedReferer = HttpContext.Current.Request.UrlReferrer.ToString();
在 VS 上;这些是更改 UrlReferrer 前后的值:
initialReferer
"http://localhost/Test/Default.aspx"
fakedReferer
"http://example.com/"
如果您使用ILSpy打开 System.Web 程序集,您会注意到 UrlReferrer 属性如下所示:
public Uri UrlReferrer
{
get
{
if (this._referrer == null && this._wr != null)
{
string knownRequestHeader = this._wr.GetKnownRequestHeader(36);
if (!string.IsNullOrEmpty(knownRequestHeader))
{
try
{
if (knownRequestHeader.IndexOf("://", StringComparison.Ordinal) >= 0)
{
this._referrer = new Uri(knownRequestHeader);
}
else
{
this._referrer = new Uri(this.Url, knownRequestHeader);
}
}
catch (HttpException)
{
this._referrer = null;
}
}
}
return this._referrer;
}
}