About the picture hotlinking this problem, after all, is the success of their own work, many people do not want others to steal it so easily. This feature is on many forums, probably because hotlinking has too much to act on.
Anti-hotlinking program is actually very simple, familiar with the ASP.net application lifecycle words can easily write a, the use of HttpModule in the BeginRequest event interception request on the OK, the rest of the work is filtered, and then filtered!
If you are not familiar with HttpModule, you can go to the MSDN lookup, Introduction very detailed, Address: Ms-help://ms. Vscc.v80/ms. Msdn.v80/ms. Visualstudio.v80.chs/dv_aspnetcon/html/f1d2910f-61d0-4541-8af8-c3c108ca351f.htm, there's no more nonsense here.
Copy Code code as follows:
private void Application_BeginRequest (Object source, EventArgs E)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = Application. context;
bool Issafe = true; is legal link
String uri = context. Request.Url.AbsolutePath.ToLower ();
if (URI. LastIndexOf (".") ) > 0 && context. Request.urlreferrer!= null)
{
String exp = URI. Substring (Uri.lastindexof (".") ));
This is to determine if the file suffix name is within the excluded file type list
BOOL Ishas = ClassLibrary.RData.RString.StrIsIncUseSC (exp, config. Imgsafetype.split (' | ') ));
if (Ishas)
{
String domainoutter = context. Request.UrlReferrer.Authority.ToLower ();//include domain name and port
ArrayList Arry = Common.Cache.GetDomainValid ()//To take a system-defined legal domain name binding list
Issafe = Arry. Contains (domainoutter);//Determine whether the current requested domain name is within the legal list
}
}
The following is illegal when the output, if there is a default alternative to the picture output, if not on the generation of a, format. Gif
if (! Issafe)
{
Bitmap img = null;
Graphics g = null;
MemoryStream ms = NULL;
Try
{
String picpath = ClassLibrary.RPath.GetFullDirectory ("Images/unlawful.gif");
if (file.exists (Picpath))
{
img = new Bitmap (Picpath, false);
}
Else
{
img = new Bitmap (* *, * *);
g = Graphics.fromimage (IMG);
G.clear (Color.White);
Font f = new font ("Arial, Blackbody, Arial", 9,fontstyle.bold);
SolidBrush s = new SolidBrush (color.red);
g.DrawString (Resources.Message.LawlessLink, F, S, 1, 20);
Img. Save (Picpath, imageformat.gif);
}
ms = new MemoryStream ();
Img. Save (MS, Imageformat.gif);
Context. Response.clearcontent ();
Context. Response.ContentType = "Image/gif";
Context. Response.BinaryWrite (Ms.toarray ());
Context. Response.End ();
}
Catch
{ }
Finally
{
if (g!= null)
G.dispose ();
Img. Dispose ();
}
}
}
All benefits will be harmful, the biggest drawback is to increase the system overhead, the client every request to filter again, the performance of nature to discount. Do not know which friend has a better way, or the optimization method, together to explore
the function of implementing file-putting hotlinking is renewed:
First add a global file global.asax
In Application_BeginRequest we can determine whether the Urlreferre in the HTTP header is sourced from this site.
Copy Code code as follows:
if (HttpContext.Current.Request.UrlReferrer!= null)
{
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith ("JPG", stringcomparison.ordinalignorecase) && HttpContext.Current.Request.UrlReferrer.Host!= "localhost")
{
HttpContext.Current.Response.WriteFile (HttpContext.Current.Server.MapPath ("~/jzdl.jpg"));
HttpContext.Current.Response.End ();
}
}