Sunday, January 20, 2013

Encrypt Connection String in Web.config

There are two methods are available to provide security for the connection string in web.cofig.

1) RSAProtectedConfigurationProvider
2) DataProtectionConfigurationProvider

Include the namespace in class file
using System.Web.Configuration;
using System.Web.Security;
using System.Configuration;

 //If you want to use RSA Protection just replace "DataProtectionConfigurationProvider" to //"RSAProtectedConfigurationProvider"
       
        SqlConnection conStr = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString);

protected void btnEncrypt_Click(object sender, EventArgs e)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection section = config.GetSection("connectionStrings");
            if (!section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                config.Save();
            }
        }

        protected void btnDecrypt_Click(object sender, EventArgs e)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection section = config.GetSection("connectionStrings");
            if (section.SectionInformation.IsProtected)
            {
                section.SectionInformation.UnprotectSection();
                config.Save();
            }
        }
    }


Saturday, January 19, 2013

Calculator in ASP.NET

Display Windows Calculator in ASP.NET on Button Click Event.

Include the name space in you webform and Write the code on Button Click. System.Diagnostics.Process.Start("Application Name");

Process.Start invokes the process from system32 directory.

using System.Diagnostics;

 protected void btnCalculator_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }


Print Functionality on Button Click using JavaScript

<asp:Button ID="printButton" runat="server" Text="Print"             OnClientClick="javascript:window.print();" Width="70px"
            onclick="printButton_Click" />