[Asp.net mvc odd tricks] 05, asp. netmvc05

Source: Internet
Author: User

[Asp.net mvc odd tricks] 05, asp. netmvc05
I. Requirements:

In web development, some javascript problems are often handled, including javascript compression, merge, release version, and obfuscation encryption. In asp.net development, we can use ScriptBundle to solve most javascript problems, includingAnd compress the released version..

For simple ScriptBundle applications, refer

Asp.net mvc4 use System. Web. Optimization to introduce javascript and style, optimize code merging and compression (ScriptBundle, StyleBundle, Bundling and Minification)

Official documentation Bundling and Minification

 

The ScriptBundle provided by asp.net already contains most of the functions, but cannot implement javascript obfuscation encryption. In actual projects, it is true that many users do not want to directly view the javascript source code.Copy and paste the javascript code to the obfuscation-encrypted website, and then perform the obfuscation encryption.. This method is inefficient and prone to unpredictable problems.

Common Use of javascript obfuscation to encrypt websites

Http://tool.lu/js/

Http://tool.chinaz.com/js.aspx

Http://www.sojson.com/jscodeconfusion.html

Since asp.net ScriptBundle has already optimized so many javascript operations for us, it is impossible to extend ScriptBundle and add a obfuscation encryption. The answer is yes.

 

2. Search for ScriptBundle extension points

First, use ilspy to find the source code and the extension point. The first step is to open the dll of System. Web. Optimization and then query ScriptBundle.

In the decompiled ScriptBundle class, some key information (JsMinify) is found, that is, a custom class CustomScriptBundle inherits Bundle, then, input the custom obfuscation encryption js class CustomJsMinify that implements the IBundleTransform interface in the constructor.

This sentence sounds a bit difficult.

1. Create a CustomScriptBundle class to inherit Bundle
    public class CustomScriptBundle : Bundle    {        public CustomScriptBundle(string virtualPath) : this(virtualPath, null)        {        }        public CustomScriptBundle(string virtualPath, string cdnPath) : base(virtualPath, cdnPath, new IBundleTransform[]        {           new CustomJsMinify()        })        {            base.ConcatenationToken = ";";        }    }

 

2. Create a CustomJsMinify class to implement the IBundleTransform interface and implement the Process

The source code of javascript to be processed is in the Process method,Here we can compress the javascript source code, confuse encryption, or add copyright..

The following code adds a simple copyright to each javascript file.

    public class CustomJsMinify : IBundleTransform    {        public void Process(BundleContext context, BundleResponse response)        {            if (context == null)            {                throw new ArgumentNullException("context");            }            if (response == null)            {                throw new ArgumentNullException("response");            }            if (!context.EnableInstrumentation)            {                try                {                    response.Content = "/*\r\nAuthor:Emrys\r\nVerson:1.1\r\n*/\r\n" + response.Content;                }                catch (Exception ex)                {                    response.Content = "/*\r\nError:" + ex.Message + "\r\n*/\r\n" + response.Content;                }            }            response.ContentType = "text/javascript";        }    }
3. Use CustomScriptBundle When configuring javascript in BundleConfig
bundles.Add(new CustomScriptBundle("~/bundles/layout").Include(                        "~/Scripts/layout.js"                        ));

 

4. the method used in cshtml remains unchanged.
 @Scripts.Render("~/bundles/layout")

 

The result of running the project is:

Add custom text at the beginning of javascript code.

When running the project, remember to set the project web. change debug in config to false, because by default, javascript code in debug mode is not processed, because if compression is combined or mixed encryption is performed in debug state, it is difficult for programmers to debug js Code.

5. Summary

Create a new class to implement the IBundleTransform interface and implement the method Process, and perform the necessary processing in the method Process.

 

3. Search for C # Obfuscated javascript code or API

We already know how to extend ScriptBundle, so now we need to find C # code or APIs that reasonably confuse and encrypt javascript.

1. http://dean.edwards.name/download/#packer can realize obfuscation encryption of js, open source free

2. http://www.javascriptobfuscator.com/A obfuscated encryption website abroad, advanced features require charges

3. A https://github.com/aemkei/jsfuck can convert javascript code[] ()! +Character

4. https://github.com/mishoo/UglifyJS2

You can select different methods based on your needs. Generally, you can select the first method. If special encryption is required. You can use the second method.

 

Let's take the first example.

First download the code, and then follow the above method to finally implementProcessMethod for processing javascript

   public class CustomJsMinify : IBundleTransform    {        public void Process(BundleContext context, BundleResponse response)        {            if (context == null)            {                throw new ArgumentNullException("context");            }            if (response == null)            {                throw new ArgumentNullException("response");            }            if (!context.EnableInstrumentation)            {                try                {                    ECMAScriptPacker p = new ECMAScriptPacker(ECMAScriptPacker.PackerEncoding.Normal, true, true);                    response.Content = p.Pack(response.Content);                }                catch (Exception ex)                {                    response.Content = "/*\r\nError:" + ex.Message + "\r\n*/\r\n" + response.Content;                }            }            response.ContentType = "text/javascript";        }    }

 

The javascript result is:

Iv. Summary

By extending ScriptBundle, we can easily confuse and encrypt the website's javascript code, protecting javascript code.

We can perform obfuscation encryption, compression, and other operations on javascript in many ways, such as IHttpModule or javascript.

Advantages of ScriptBundle Extension

1. You can continue to use the ScriptBundle compression, merge, and version control functions, but these functions further add the obfuscation encryption function.

2. You do not need to paste and copy to an encrypted website at each release. Now you can display all the encrypted websites automatically.

3. Using excellent encryption sdks can protect the javascript source code more effectively.

4. You can operate on javascript at will, such as adding the copyright version number.

 

Github: https://github.com/Emrys5/Asp.MVC-05-Extended-ScriptsBundle/tree/master

Oschina: http://git.oschina.net/emrys5/Asp.MVC-05-Extended-ScriptsBundle

This article is original. You are welcome to shoot bricks andRecommendation.

Series courses
  • [Asp.net mvc odd tricks] 01-encapsulation context-Get custom context in View
  • [Asp.net mvc odd tricks] 02-use the Razor engine to generate Html code in the Action
  • [Asp.net mvc odd tricks] 03-enumeration feature extensions solve enumeration naming problems and support HtmlHelper
  • [Asp.net mvc odd tricks] 04-Do you actually use the Action model to bind it?
  • [Asp.net mvc odd tricks] 05-added ScriptBundle and supported obfuscation encryption javascript

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.