INITIAL CHAMBER

For Web Form

For SQL Server Database

DESIGN CHAMBER

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:

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. // Your encrypted string
  15. </CipherValue>
  16. </CipherData>
  17. </EncryptedData>
  18. </connectionStrings>
  19. </configuration>
configuration

Hope you liked this. Thank you for reading. Have a good day.