Premier Web Design, Web Development and eCommerce Development in Virginia, Washington DC and Maryland

Ameronix is a web design and development firm capable of planning, developing and maintaining basic websites to advanced websites ranging from informative websites to eCommerce platforms and social networking websites to high end corporate intranet/portal websites.
Ameronix is a web design and development firm capable of planning, developing and maintaining basic websites to advanced websites ranging from informative websites to eCommerce platforms and social networking websites to high end corporate intranet/portal websites.

Archive for the ‘Tutorials and More’ Category

Script Tags and Master Pages in ASP

Saturday, February 23rd, 2008

Introduction

In a recent ASP .NET project I decided to use a global “Master Page” to contain my major theme code and use individual ASPX pages strictly for content. As a personal preference I like to use absolute paths relative to the site root. I very rarely have path issues when using absolute paths. This project requires that relative paths be used since the web application would be switching domains and placed within a sub-directory. One of the advantages of Master pages is that any pages in a sub-directory that utilize a Master page in the application’s root directory have their relative links updated automatically. The server adds the correct number of “../”’s to the front of the path. According to the MSDN documentation this occurs on all ASP web controls (anything that is <asp:….>). I noticed that my <link> paths were also automatically being updated, neat.

The Problem:

The problems started to show up when I added JavaScript to the Master page for navigation. Everything worked fine in the application’s root directory (also the location of the Master Page file), but for pages in a sub-directory the script tag’s “src” attribute was not updating. Thus my navigation did not work and any other scripts would not load.

The Solution:

To rectify this error I had to write a custom server control. Having never done this before I hit some problems, but I will go through all of the steps to get the custom control created and working.

  1. Create a new Class in your project, name it “Script.cs”, it should be placed in the App_Code directory.
  2. Import the necessary namespaces and declare your namespace (this is very important)
    using System;
    using System.ComponentModel;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace Ameronix
  3. Next declare a couple of enums for the script type and script language, these will make future expansion easier.
    
    public enum ScriptType { javascript };
    
    public enum ScriptLanguage { javascript };
  4. Since this is a control that is not visible to the user we are extending the .NET class “Control”. We also need to set some security levels and toolbox data.
    
        [
    
        AspNetHostingPermission(SecurityAction.Demand,
    
            Level = AspNetHostingPermissionLevel.Minimal),
    
        AspNetHostingPermission(SecurityAction.InheritanceDemand,
    
            Level = AspNetHostingPermissionLevel.Minimal),
    
        DefaultProperty("ScriptSource"),
    
        ToolboxData("<{0}:Javascript runat\"server\" />")
    
        ]
    
        public class Script : Control
  5. Next we declare our properties that will be accessible. I have included the Script’s source, type, and language, as these are the most common.
    
            /// <summary>
    
            /// The source url for the script
    
            /// </summary>
    
           [
    
            Bindable(true),
    
            Category("Behavior"),
    
            DefaultValue(""),
    
            Description("The relative path to the javascript file."),
    
            Localizable(false)
    
            ]
    
            public string ScriptSource
    
            {
    
                get { return ViewState["ScriptSource"] as string; }
    
                set { ViewState["ScriptSource"] = value; }
    
            }        /// <summary>
    
            /// The type of script this is
    
            /// </summary>
    
            public ScriptType ScriptType
    
            {
    
                get { return (ViewState["ScriptType"] == null) ? ScriptType.javascript : (ScriptType)ViewState["ScriptType"]; }
    
                set { ViewState["ScriptType"] = value; }
    
            }
    
    /// <summary>
    
            /// The language the script is written in
    
            /// </summary>
    
            public ScriptLanguage ScriptLanguage
    
            {
    
                get { return (ViewState["ScriptLanguage"] == null) ? ScriptLanguage.javascript : (ScriptLanguage)ViewState["ScriptLanguage"]; }
    
                set { ViewState["ScriptLanguage"] = value; }
    
            }
  6. Finally we override the Render() method so we can actually output the element.
    
    
            /// <summary>
    
            /// Renders the control at runtime
    
            /// </summary>
    
            /// <param name="writer">The writer to use to output the HTML</param>
    
            protected override void Render(HtmlTextWriter writer)
    
            {
    
                base.Render(writer);            string type;
    
                string language;
    
    switch (ScriptType)
    
                {
    
                    case ScriptType.javascript:
    
                    default:
    
                        type = “text/javascript”;
    
                        break;
    
                }
    
    switch (ScriptLanguage)
    
                {
    
                    case ScriptLanguage.javascript:
    
                    default:
    
                        language = “javascript”;
    
                        break;
    
                }
    
    writer.Write(”<script src=\”{0}\” type=\”{1}\” language=\”{2}\”></script>”, ResolveClientUrl(ScriptSource), type, language);
    
    writer.Write(Environment.NewLine);
    
            }

    The most important line converts our relative link for any page underneath a sub-directory.

    
    
    writer.Write("<script src=\"{0}\" type=\"{1}\" language=\"{2}\"></script>", ResolveClientUrl(ScriptSource), type, language);
    
    

    The code should resemble the following:

    
    
    using System;
    
    using System.ComponentModel;
    
    using System.Security.Permissions;
    
    using System.Web;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;namespace Ameronix
    
    {
    
    public enum ScriptType { javascript };
    
        public enum ScriptLanguage { javascript };
    
    [
    
        AspNetHostingPermission(SecurityAction.Demand,
    
            Level = AspNetHostingPermissionLevel.Minimal),
    
        AspNetHostingPermission(SecurityAction.InheritanceDemand,
    
            Level = AspNetHostingPermissionLevel.Minimal),
    
        DefaultProperty(”ScriptSource”),
    
        ToolboxData(”<{0}:Javascript runat\”server\” />”)
    
        ]
    
        public class Script : Control
    
        {
    
            /// <summary>
    
            /// The source url for the script
    
            /// </summary>
    
           [
    
            Bindable(true),
    
            Category(”Behavior”),
    
            DefaultValue(”"),
    
            Description(”The relative path to the javascript file.”),
    
            Localizable(false)
    
            ]
    
            public string ScriptSource
    
            {
    
                get { return ViewState[”ScriptSource”] as string; }
    
                set { ViewState[”ScriptSource”] = value; }
    
            }
    
    /// <summary>
    
            /// The type of script this is
    
            /// </summary>
    
            public ScriptType ScriptType
    
            {
    
                get { return (ViewState[”ScriptType”] == null) ? ScriptType.javascript : (ScriptType)ViewState[”ScriptType”]; }
    
                set { ViewState[”ScriptType”] = value; }
    
            }
    
    /// <summary>
    
            /// The language the script is written in
    
            /// </summary>
    
            public ScriptLanguage ScriptLanguage
    
            {
    
                get { return (ViewState[”ScriptLanguage”] == null) ? ScriptLanguage.javascript : (ScriptLanguage)ViewState[”ScriptLanguage”]; }
    
                set { ViewState[”ScriptLanguage”] = value; }
    
            }
    
    /// <summary>
    
            /// Renders the control at runtime
    
            /// </summary>
    
            /// <param name=”writer”>The writer to use to output the HTML</param>
    
            protected override void Render(HtmlTextWriter writer)
    
            {
    
                base.Render(writer);
    
    string type;
    
                string language;
    
    switch (ScriptType)
    
                {
    
                    case ScriptType.javascript:
    
                    default:
    
                        type = “text/javascript”;
    
                        break;
    
                }
    
    switch (ScriptLanguage)
    
                {
    
                    case ScriptLanguage.javascript:
    
                    default:
    
                        language = “javascript”;
    
                        break;
    
                }
    
    writer.Write(”<script src=\”{0}\” type=\”{1}\” language=\”{2}\”></script>”, ResolveClientUrl(ScriptSource), type, language);
    
    writer.Write(Environment.NewLine);
    
            }
    
        }
    
    }
  7. Now we have a new control, but our ASP pages cannot see it. We need to map the namespace in our application. Open your web.config file and add the following to your system.web directive.
    
    
          <pages>
    
            <controls>
    
              <add tagPrefix="amx" namespace="Ameronix"/>
    
            </controls>
    
          </pages>
    
    

    The tag prefix will be useful shortly so don’t forget it.

  8. In your master page file start a new tag “<amx:” auto complete should display “Script” now. Fill in the required variables and your new control will be used in the master page. Note: you MUST include runat=”server” or the control will not be converted into the appropriate script tag. For example:
    
    
    <amx:Script ID="js_jquery" ScriptSource="resources/js/jquery.js" ScriptType="javascript" ScriptLanguage="javascript" runat="server" />
    
    

An Introduction to Encryption and It’s Uses : an MCrypt story

Tuesday, February 5th, 2008

Many if not most current websites handle sensitive data in one form or another. Be it email addresses, passwords, credit card information or some other form of data, visitors to websites demand that thier information be kept confidential. As developers it is our job to uphold visitor expectations. This is a vexing problem considering the rise of internet piracy and credit fraud. However, there are is a tool available to developers that can help protect both your and your clients interests: encryption. One of the safest ways to store sensitive data securely is through the use of encryption. The following describes the use and features of encryption using the tool: Mcrypt (an open source third party encryption library written for the PHP language).Encryption is the process of converting plain text information into ciphertext (text unreadable by humans) through the use of algorithms and functions known as ciphers. MCrypt offers a wide variety of encryption ciphers, choosing the correct cipher for your application may require some further research into pros and cons of each. Something to keep in mind, however, is that encryption can either be one-way or two-way, meaning once the ciphertext is created it can either not be decrypted, or can be reformed into plaintext by using a decryption cipher. For example, a two way encryption cipher such as MCRYPT_3DES (Triple DES) would be best for storing credit card information, as the plaintext credit card number is needed to process a transaction.

Another decision that needs to be made is which block cipher mode to utilize with your encryption cipher. A cipher using a constant key and plaintext will always return the same ciphertext. To provide more security block cipher modes use Initialization Vectors (IVs) to process the ciphertext into a randomized constant length string. The four main block cipher modes supported by MCrypt are Cipher Block Chaining (CBC), Output Feedback (OFB), Cipher Feedback (CFB), and Electronic Codebook (ECB). These four modes are geared towards confidentiality. reference

Initialization Vectors are required for use in block cipher modes. They allow a block cipher to process a string of plaintext into a block of cipher text unique from every other block of ciphertext. The size of the IV coorelates to the block cipher you chose for your encryption scheme. MCrypt provides a useful method of retrieving the size of this string called mcrypt_get_iv_size, which takes into account the cipher and encryption mode as parameters, and returns the length of the required IV. Note: Mode ECB does not require an IV.

Lastly and perhaps most importantly, an Encryption Key needs to be chosen for the application. All of the Ciphers available through MCrypt are built on open standards. For the application to obtain actual security, a secret key needs to be created for the encryption scheme. “This principle is known as Kerckhoffs’ principle - ‘only secrecy of the key provides security’”.

After choosing the encryption scheme (cipher, mode, iv, and key) that best fit the website application, encrypting and decrypting is a simple matter of calling the library’s mcrypt_encrypt and mcrypt_decrypt methods.