我使用以下代码实现了这一点:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var ClientSourceIP = Context.Request.Headers["CLIENT_SRC_IP"];
var HTTPClientSourceIP = Context.Request.Headers["HTTP_CLIENT_SRC_IP"];
var isValidClientSourceIP = ClientSourceIP == null ||
Regex.IsMatch(ClientSourceIP, ConfigurationManager.AppSettings["ValidClientSourceIP"]);
var isValidHTTPClientSourceIP = HTTPClientSourceIP == null ||
Regex.IsMatch(HTTPClientSourceIP, ConfigurationManager.AppSettings["ValidHTTPClientSourceIP"]);
if (
(Context.Request.Path.Contains("elmah.axd") || Context.Request.Path.Contains("elvue.aspx")) &&
((isValidClientSourceIP && isValidHTTPClientSourceIP) == false)
)
{
this.Context.Response.StatusCode = 403;
return;
}
}