ASP.net中实现基于UrlRewrite的防盗链功能
2014-03-13 17:37
543 查看
void Application_BeginRequest(object sender, EventArgs e)
{
bool IsAllowDomain = false;
bool IsLogin = false;
string CookiesName = "UserName", AllowHost, ReferrerHost="";
int CheckType = 1;
bool AllowDown = false;
string[] AllowHostArr;
string UrlPattern = "", UrlReplace = "";
string[] pattern, replace;
string ConfigFile = ConfigurationManager.AppSettings["DownLoadConfig"];
if (ConfigFile != "")
{
try
{
System.Xml.XmlDataDocument XDConfig = new System.Xml.XmlDataDocument();
XDConfig.Load(AppDomain.CurrentDomain.BaseDirectory + @"/" + ConfigFile);
if (XDConfig.SelectSingleNode("DownLoad/CheckType").InnerText != "")
{
CheckType = int.Parse(XDConfig.SelectSingleNode("DownLoad/CheckType").InnerText);
}
if (XDConfig.SelectSingleNode("DownLoad/CookiesName").InnerText != "")
{
CookiesName = XDConfig.SelectSingleNode("DownLoad/CookiesName").InnerText;
}
AllowHost = XDConfig.SelectSingleNode("DownLoad/AllowHost ").InnerText;
AllowHostArr = AllowHost.Split('|');
UrlPattern = XDConfig.SelectSingleNode("DownLoad/UrlPattern").InnerText;
UrlReplace = XDConfig.SelectSingleNode("DownLoad/UrlReplace").InnerText;
pattern = UrlPattern.Split('@');
replace = UrlReplace.Split('@');
if (CookiesName == "") CookiesName = "UserName";
IsLogin = false.Equals(Request.Cookies[CookiesName] == null || Request.Cookies[CookiesName].Value == "");
if (Request.UrlReferrer != null) ReferrerHost = Request.UrlReferrer.Host.ToString();
if (AllowHostArr.Length < 1)
{
IsAllowDomain = true;
}
else
{
for (int HostI = 0; HostI < AllowHostArr.Length - 1; HostI++)
{
if (AllowHostArr[HostI].ToLower() == ReferrerHost.ToLower())
{
IsAllowDomain = true;
break;
}
}
}
switch (CheckType)
{
case 1:
AllowDown = true.Equals(IsAllowDomain);
break;
case 2:
AllowDown = IsLogin;
break;
case 3:
AllowDown = true.Equals(IsAllowDomain && IsLogin);
break;
}
if (AllowDown == false)
{
string oldUrl = HttpContext.Current.Request.RawUrl;
string newUrl = oldUrl;
for (int iii = 0; iii < pattern.Length; iii++)
{
if (Regex.IsMatch(oldUrl, pattern[iii], RegexOptions.IgnoreCase | RegexOptions.Compiled))
{
newUrl = Regex.Replace(oldUrl, pattern[iii], replace[iii], RegexOptions.Compiled | RegexOptions.IgnoreCase);
oldUrl = newUrl;
}
}
this.Context.RewritePath(newUrl);
}
}
catch
{
}
}
}
相关文章推荐
- ASP.net中实现基于UrlRewrite的防盗链功能
- ASP.net中实现基于UrlRewrite的防盗链
- [转]:ASP.net中实现基于UrlRewrite的防盗链
- 基于ASP.NET+easyUI框架实现图片上传功能(判断格式+即时浏览 )
- asp.net基于Calendar实现blog日历功能示例
- 如何基于asp.net实现ListBox控件的鼠标拖拽功能
- 基于asp.net实现图片在线上传并在线裁剪功能
- 使用FlexBox和Json实现类似ComboBox(类似Google的输入提示和自动)功能-基于JQuery-ASP.NET
- 基于ASP.NET+EasyUI框架实现图片上传提交表单功能(js提交图片)
- 基于ASP.NET+EasyUI框架实现图片上传提交表单功能(js提交图片)
- 基于ASP.NET+easyUI框架实现图片上传功能(判断格式+即时浏览 )
- 基于asp.net的注销功能的实现
- 基于ASP.NET+easyUI框架实现图片上传功能(表单)
- Asp.net网站使用HttpHandler实现图片防盗链功能
- ASP.net UrlRewrite的防盗链功能
- 基于ASP.NET+easyUI框架实现图片上传功能(表单)
- [笔记] ASP.NET本身的缓存实现机制,基于Cache的实现。提供按分类进行管理的缓存功能
- ASP.NET CheckBoxList 空间实现全选、反选、清除功能
- ASP.NET手机短信功能实现
- ASP.NET中实现类似MSN消息提示功能