在用asp.net过程中想判断一下用户是不是从外部提交网址,在搜索上没有找到愿意答案,自己做了一个,模仿asp中的做法,源码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!IsExternal())
{
Response.Write("不是外部提交");
}
else
{
Response.Write("这是外部提交");
}
}
}
/// <summary>
/// 判断是否外部提交
/// </summary>
/// <returns>true:是、flase:否</returns>
public static bool IsExternalPost()
{
string servername = Convert.ToString(HttpContext.Current.Request.ServerVariables["SERVER_NAME"]);
string referer = Convert.ToString(HttpContext.Current.Request.ServerVariables["Http_Referer"]);
string strMidReferer = referer.Substring(7, referer.Length - 7);
if (!string.IsNullOrEmpty(referer))
{
if (strMidReferer.IndexOf("/") > 0)
{
strMidReferer = strMidReferer.Substring(0, strMidReferer.IndexOf("/"));
}
if (strMidReferer != servername) //外部提交
return true;
else
return false;
}
else
return true;
}