How To Make Your Connection String Encrypted In ASP.NET Using C#

INITIAL CHAMBER

  • Open Visual Studio 2010 and create an empty website. Give a suitable name connectionstring_demo.
  • In Solution Explorer you get your empty website. Add a web form, SQL Database. Here are the steps:

For Web Form

  • connectionstring _demo (Your Empty Website) - Right Click, Add New Item, click Web Form. Name it connectionstring _demo.aspx.

For SQL Server Database

  • connectionstring _demo (Your Empty Website) - Right Click, Add New Item, click SQL Server Database. Add Database inside the App_Data_folder.

DESIGN CHAMBER

  • Now open your connectionstring _demo.aspx file, where we create our design for encrypting our Connection String.
connectionstring _demo.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
  12. Text="Press to Encrypt your Connection String" />  
  13.             </div>  
  14.             <p>  
  15.  </p>  
  16.             <p>  
  17.  </p>  
  18.             <p>  
  19.                 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  20.             </p>  
  21.         </form>  
  22.     </body>  
  23. </html>  

CODE CHAMBER:

  • Open your connectionstring _demo.aspx.cs and write some code so that our application starts working. 
connectionstring _demo.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Configuration;  
  7. using System.Web.Configuration;  
  8. using System.Web.UI.WebControls;  
  9. public partial class _Default: System.Web.UI.Page  
  10. {  
  11.     const string PROVIDER = "DataProtectionConfigurationProvider";  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {}  
  14.     protected void Button1_Click(object sender, EventArgs e)  
  15.     {  
  16.         Configuration con = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);  
  17.         ConnectionStringsSection sect = con.ConnectionStrings;  
  18.         sect.SectionInformation.ProtectSection(PROVIDER);  
  19.         con.Save();  
  20.         Label1.Text = "Your Connection String is Encrypted Now";  
  21.         Label1.Text += "Your Connection String is:" + ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString;  
  22.     }  
  23. }  

OUTPUT CHAMBER

output chamber

Open your Web.config File

  1. <?xml version="1.0"?>  
  2. <!--  
  3. For more information on how to configure your ASP.NET application, please visit  
  4. http://go.microsoft.com/fwlink/?LinkId=169433  
  5. -->  
  6. <configuration>  
  7.     <system.web>  
  8.         <compilation debug="false" targetFramework="4.0" />  
  9.     </system.web>  
  10.     <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">  
  11.         <EncryptedData>  
  12.             <CipherData>  
  13.                 <CipherValue>  
  14.   
  15. // Your encrypted string  
  16.   
  17.   
  18. </CipherValue>  
  19.             </CipherData>  
  20.         </EncryptedData>  
  21.     </connectionStrings>  
  22. </configuration>  
configuration
 
Hope you liked this. Thank you for reading. Have a good day.

Up Next
    Ebook Download
    View all
    Learn
    View all