Construction of ASP.net mvc4+ef5+easyui+unity2.x injection background Management system (12)

Source: Internet
Author: User
Tags filter empty exception handling html tags sql injection system log tostring trim

System log and exception handling ②

In the last lecture we did a list of the results of the log and the exception, this section we are talking about to put in his application system.

First we create a generic class Resulthelper in the App.common class library, this class is written inside, gets a GUID, gets the current time, handles strings, and so on, although one of the things we're going to use now is to get the GUID, but we might have to use something else later, so I put him in.

Then in App.admin to create a core folder, cores, put loghandler This class is mainly written to the log, to avoid the instantiation of this class every time, I encapsulated him, we can see that.

Then modify the controller create method, as shown in the code below one by one.

Using System;
Using System.Web;
    
Using System.Text.RegularExpressions;
        Namespace App.common {public class Resulthelper {///<summary>///Create a globally unique 32-bit ID
            </summary>///<returns>id string </returns> public static string NewId {
                get {String id = DateTime.Now.ToString ("Yyyymmddhhmmssfffffff"); String guid = Guid.NewGuid (). ToString ().
                Replace ("-", ""); id = = GUID.
                Substring (0, 10);
            return ID; } public static string Newtimeid {get {string id = Dat
                ETime.Now.ToString ("Yyyymmddhhmmssfffffff");
            return ID; }///<summary>///intercept string///</summary>///<param name= "value" & gt; string </param>///<param name= "Length" > Remaining length </param>///<reTurns> the specified string and adds ...</returns> public static string Subvalue (string value, int length) { if (value. Length > Length) {value = value. Substring (0, length); Value = value + "...";
            return nohtml (value);
        else {return nohtml (value);} When//restore public static string Inputtext (string inputstring) {if (inputstring!
                = null) && (inputstring!= String.Empty)) {inputstring = Inputstring.trim (); 
                if (Inputstring.length > maxLength)//inputstring = inputstring.substring (0, maxLength);
                InputString = Inputstring.replace ("<br>", "\ n");
                InputString = Inputstring.replace ("&", "&amp");
                InputString = Inputstring.replace ("'", "" ");
                InputString = Inputstring.replace ("<", "&lt"); InputString = INPUtstring.replace (">", "&gt");
                InputString = Inputstring.replace ("Chr", "&lt");
                InputString = Inputstring.replace ("Chr (Panax Notoginseng)", "&gt");
                InputString = Inputstring.replace ("\" "," &quot ");
    
                InputString = Inputstring.replace (";", ";");
            return inputstring;
            else {return ""; When//Add public static string Outputtext (string outputstring) {i F ((outputstring!= null) && (outputstring!= String.Empty)) {outputstring = Outputst Ring.
                Trim ();
                outputstring = Outputstring.replace ("&amp", "&");
                outputstring = Outputstring.replace ("" "," "");
                outputstring = Outputstring.replace ("&lt", "<");
                outputstring = Outputstring.replace ("&gt", ">"); OutputString = Outputstring.replace ("&lt", "Chr (60)");
                outputstring = Outputstring.replace ("&gt", "Chr (37)");
                outputstring = Outputstring.replace ("&quot", "\");
                outputstring = Outputstring.replace (";", ";");
                outputstring = Outputstring.replace ("\ n", "<br>");
            return outputstring;
            else {return ""; }}///<summary>///to remove HTML tags///</summary>///<param name= "Noht ML "> including HTML source </param>///<returns> has been removed after the text </returns> public static string nohtml (St Ring htmlstring) {//delete script htmlstring = Regex.Replace (htmlstring, @ "<script[^>]*?>
            .*?</script> "," ", regexoptions.ignorecase); Delete html htmlstring = Regex.Replace (htmlstring, @ "<" (. [
^>]*) > "," ", regexoptions.ignorecase);            htmlstring = Regex.Replace (htmlstring, @ "([\ r \ n]) [\s]+", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "-->", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "<!--. *", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (quot| #34);", "\", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (amp| #38);", "&", Regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (lt| #60);", "<", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (gt| #62);", ">", Regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (nbsp| #160);", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (iexcl| #161);", "\xa1", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (cent| #162);", "\xa2", RegExoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (pound| #163);", "\xa3", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "& (copy| #169);", "\xa9", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "&# (\d+);", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "&hellip;", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "&mdash;", "", regexoptions.ignorecase);
            htmlstring = Regex.Replace (htmlstring, @ "&ldquo;", "", regexoptions.ignorecase);
            Htmlstring.replace ("<", "");
            htmlstring = Regex.Replace (htmlstring, @ "&rdquo;", "", regexoptions.ignorecase);
            Htmlstring.replace (">", "");
            Htmlstring.replace ("\ r \ n", ""); htmlstring = HttpContext.Current.Server.HtmlEncode (htmlstring).
            Trim ();
    
        return htmlstring; }///&Lt;summary>///Formatted text (prevent SQL injection)///</summary>///<param name= "str" ></param> <returns></returns> public static string Formatstr (string html) {Syste M.text.regularexpressions.regex regex1 = new System.Text.RegularExpressions.Regex (@ "<script[\s\s]+</script *
            > ", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex (@ "href *= *[\s\s]*script *:",
            System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex (@ "on[\s\s]*=",
            System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex (@ "<iframe[\s\s]+</
            IFrame *> ", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.Regularexpressions.regex regex5 = new System.Text.RegularExpressions.Regex (@ "<frameset[\s\s]+</frameset *>
            ", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex10 = new System.Text.RegularExpressions.Regex (@ "select")
            System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex11 = new System.Text.RegularExpressions.Regex (@ "Update"),
            System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex12 = new System.Text.RegularExpressions.Regex (@ "Delete")
            System.Text.RegularExpressions.RegexOptions.IgnoreCase); html = regex1. Replace (HTML, ""); Filter <script></script> Tag html = regex2. Replace (HTML, ""); Filter Href=javascript: (<A>) attribute html = regex3. Replace (HTML, "_disibledevent="); Filter other controls on ... Event HTML = regex4. Replace (HTML, ""); Filter iframe HTML = regex10.Replace (HTML, "S_elect"); html = regex11.
            Replace (HTML, "U_pudate"); html = regex12.
            Replace (HTML, "D_elete"); html = HTML.
            Replace ("'", "" "); html = HTML.
            Replace ("&nbsp;", "");
        return HTML; ///<summary>///Check SQL statement legality///</summary>///<param name= "SQL" ><
        /param>///<returns></returns> public static bool Validatesql (String sql, ref string msg) {if (SQL. ToLower ().
                IndexOf ("delete") > 0) {msg = "Query parameter contains illegal statement delete";
            return false; } if (SQL. ToLower ().
                IndexOf ("Update") > 0) {msg = "Query parameter contains illegal statement update";
            return false; } if (SQL. ToLower ().
                IndexOf ("Insert") > 0) {msg = "Query parameter contains illegal statement inserts";
            return false;
       }     return true; }//Get current time public static DateTime Nowtime {gets {return D
            Atetime.now; }///<summary>///converts a date to a string///</summary>///<param Name = "DT" > Date </param>///<returns> string </returns> public static string Datetimeconvertstrin
            G (DateTime DT) {if (dt = null) {return ""; else {return convert.todatetime (dt. ToString ()).
            ToShortDateString (); }///<summary>///Converts a string to a date///</summary>///<param name= "str "> String </param>///<returns> date </returns> public static DateTime?"
            Stringconvertdatetime (String str) {if (str = null) {return null; }
            else {try {return convert.todatetime (str);
                catch {return null; The public static string Getuserip () {if (system.web.httpcontext.c)}}} Urrent. request.servervariables["Http_via"]!= null) return System.Web.HttpContext.Current.Request.ServerVariables ["Http_x_forwarded_for"].
            Split (new char[] {', '}) [0];
        else return system.web.httpcontext.current.request.servervariables["REMOTE_ADDR"]; }}} Resulthelper

Related Article

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.