Asp.net C # Use global. asax

Source: Internet
Author: User

All-station anti-injection commonProgram

Create a global. asax application file under the root directory of the site, which can be completely protected against injection. The program content is as follows:

<% @ Application language = "C #" %>

<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)
{
//Code

}
Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed

}
Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs

}

Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts

}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session Mode
// If it is set to StateServer or sqlserver, this event is not triggered.

}

/// <Summary>
/// Replace all injected strings and record injection Information
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Void application_beginrequest (Object sender, eventargs E)
{
//************************
// Global anti-Injection
// Author: Yi Qiu Han
//************************

// Triggered when an application request is received.
String [] KEYWORDS = new string [] {";", "'", "--", "XP _", "XP _"};
String [] safekeys = "& #59; | & #39; | & #45; & #45; | & #120; & #112; & #95; | & #88; & #80; & #95; | & #120; & #80; & #95; | & #88; & #112; & #95; ". split ('| ');
String querystring = server. urldecode (request. querystring. tostring ());
String url = request. url. absolutepath;
// Excluded Extension
String [] dotfilename = URL. Split ('.');
String dotname = dotfilename [dotfilename. Length-1];
Dotfilename = new string [] {"axd "};
// Exit directly when the excluded extension is displayed.
Foreach (string STR in dotfilename)
{
If (STR = dotname)
Return;
}
For (INT I = 0; I <keywords. length; I ++)
{
String key = keywords [I];
If (querystring. Contains (key ))
{
//******************************
// Record injection Information
//******************************
String intorecord = system. datetime. Now. tostring ();
Intorecord + = "" + request. url. Host;
Intorecord + = "" + request. requesttype;
Intorecord + = "" + request. url. absolutepath;
Intorecord + = "" + server. urldecode (request. querystring. tostring ());
Intorecord + = "" + request. userhostaddress;
Intorecord + = "" + request. useragent;
Intorecord + = "\ r ";

Try
{
String Path = server. mappath (@ "/_ intorecordlog /");
If (! System. Io. Directory. exists (PATH ))
System. Io. Directory. createdirectory (PATH );
System. Io. file. appendalltext (path + datetime. Now. tostring ("yyyymmdd") + ". log", intorecord, system. Text. encoding. Default );
}
Catch {}
// Replace the injected URL and jump to the URL
Querystring = querystring. Replace (Key, safekeys [I]);
Response. Redirect (URL + "? "+ Querystring );
Response. End ();
}
}
}
</SCRIPT>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.