Encrypt Connection String in Web Config

Encrypt Connection String in Web Config – ASP.NET कभी भी किसी Configuration File के लिए की गई Request को पूरा नहीं करता क्योंकि Configuration Files में सामान्‍यत: Current Application से सम्बंधित Sensitive Data होते हैं।

हालांकि इस Basic Restriction के साथ ही ASP.NET हमें हमारी Configuration File के Sections के Codes को Encrypt करने की भी सुविधा Provide करता है, ताकि हम हमारे Web Application की Security को और अधिक Increase कर सकें।

Configuration File के Sections को Security के लिहाज से Encrypted रखना एक Recommend Development Approach होता है। यानी हमें हमेंशा अपनी Configuration File के उन Sections को Encrypted रखने की कोशिश करनी चाहिए, जो हमारे Web Application से सम्बंधित Sensitive Data जैसे कि Connection String, Username, Password, Unique ID आदि को Represent करते हैं।

ASP.NET मूलत: दो प्रकार के Encryption Options Provide करता है:

  • RSA Provider

RSA Provider हमें एक Key Pair Create करने की सुविधा देता है, जिसका प्रयोग बाद में Configuration Data को Encrypt करने के लिए होता है। इस Provider को Use करने का Advantage ये है कि हम इस Key को उस स्थिति में एक से ज्यादा Computers के बीच Copy कर सकते हैं जब हम एक ही Configuration File को एक से ज्यादा Computers पर समान रूप से Use करना चाहते हैं। Default रूप से ASP.NET Framework द्वारा इसी RSA Provider को Use किया जाता है।

  • DPAPI(Data Protection) Provider

DPAPI Provider हमें एक Protection Mechanism Use करने की सुविधा देता है, जो कि Windows Operating System में पहले से Built है। इसके अन्तर्गत Configuration Files एक Machine-Specific Key का प्रयोग करते हुए Encrypt होते हैं। इस Provider का Advantage ये है कि हमें अलग से किसी Key को Manage व Maintain करने की जरूरत नहीं रहती। जबकि इसका Disadvantage ये है कि इस तरीके से Encrypted Configuration File को हम एक से ज्यादा Computer System पर समान रूप से Use करने के लिए RSA Provider की तरह Copy नहीं कर सकते।

इन दोनों Options का प्रयोग करके हम अपने Web Application में Sensitive Data को Encrypt करने की सुविधा प्राप्त कर सकते हैं।

जब हम किसी Encrypted Section से किसी Setting को Retrieve करते हैं, तो ASP.NET Framework स्वयं अपने स्तर पर ही Decryption का काम कर लेता है तथा हमारे Code के लिए Plain Text Provide करता है।

इसी प्रकार से यदि हम किसी Configuration Value को Programmatically Modify करके Save करते हैं, तो उस स्थिति में उस Changed Value का Encryption भी Automatically हो जाता है। फिर भी हम हमारी Web.config File के किसी Encrypted Section को Manually Edit नहीं कर सकते।

हालांकि जब हम Configuration API को Use करते हैं, तब Decryption व Encryption Steps उस समय Automatically Perform होते हैं, जब हम Configuration File के Protected Section को Read या Write करते हैं।

Programmatic Encryption

Encryption को Programmatically Enable करने के लिए हमें Encryption से सम्बंधित ConfigurationSection.SectionInformation Object को Retrieve करना होता है और फिर उस Object के लिए ProtectSection() Method को Invoke करना होता है। किसी भी Existing Data को इसी Method के Execution द्वारा Encrypt किया जाता है और इसी Point पर किए गए किसी भी Change को ASP.NET Framework द्वारा Automatically Encrypt कर दिया जाता है।

Encryption की इस प्रक्रिया को हम निम्नानुसार Code के माध्‍यम से समझ सकते हैं जो कि हमारे Application Section को उस स्थिति में Encrypt कर देता है, जबकि वह Unencrypted हो:

// File Name:Default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;

namespace WebApp
{
publicpartialclassDefault : System.Web.UI.Page
    {
    protectedvoid Page_Load(object sender, EventArgs e)
        {
        Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
        ConfigurationSection appSettings = config.GetSection("appSettings");

        if (appSettings.SectionInformation.IsProtected)
        {
            appSettings.SectionInformation.UnprotectSection();
        }
        else
        {
        appSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
        }
        config.Save();
    }
  }
}

जैसे ही हम हमारे Code को Code-Behind File में Specify करके Run करते हैं, हमारी Web.config File में निम्नानुसार Code Add हो जाता है:

File Name:Web.config
<?xmlversion="1.0"?>
<configuration>
<appSettingsconfigProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAAvVRzGzFqkS68M5T5yN6MQQAAAACAAAAAAAQZgAAAAEAACAAAACQKUYhR1uYL4t+QRz6pJ/X/ITSkhpY6I/feErwUj97cAAAAAAOgAAAAAIAACAAAAD/+7oiC/PT996oJUCXuuHa7uw/Eq/dT7RCJaYieALRMCAAAADcWAwhFUCkKL/QoGtXEck+NaeLVE ki BLyRw2V+gyTH60AAAABtwTbLz0XFXPNOpRo3lY29Norvkxst6wRnNCwYh1YNoCIzxRED720pst7Tsf+uH4v/h5uTBf/3ArjhSfIEc3DG</CipherValue>
</CipherData>
</EncryptedData>
</appSettings>

<system.web>
<compilationdebug="true"/>
</system.web>
</configuration>

हम देख सकते हैं कि हमारी Web.config File के<appSettings> Section में लिखे गए Configuration Codes Encrypt हो गए हैं।

Command-Line Encryption

Configuration Files को Encrypt व Decrypt करने के लिए Currently कोई भी GUI Tool Available नहीं हैं। हालांकि यदि हम स्वयं Configuration File Encryption से सम्बंधित पिछले Section में Discuss किए गए Codes न लिखना चाहें, तो aspnet_regiis.exe नाम की Command-Line Utility को Use करते हुए भी इस जरूरत को पूरा कर सकते हैं, जो कि C:\Windows\Microsoft.NET\Framework\[Version] Directory में Exist होती है। इस Tool को Use करने के लिए जरूरी है कि हमने पहले से ही IIS Web Server में अपने Project के लिए Virtual Directory Create कर रखा हो।

जब हम हमारी Configuration File के किसी Portion को Protect करने के लिए aspnet_regiis.exe Tool को Use करते हैं, तब हम निम्न Command-Line Arguments Specify कर सकते हैं:

-pe ये Switch उस Configuration Section को Specify करता है, जिसे Encrypt करना होता है।
-app ये Switch हमारे Web Application के Virtual Path को Specify करता है।
-prov ये Switch हमारे Web Application के Encryption के लिए Use किए जाने वाले Provider RSA / DPAPI  को Specify करता है।

यदि हम हमारे पिछले Example की Configuration File को ही Command-Line के माध्‍यम से Encrypt करना चाहें, तो हमारा Command-Line Statement निम्नानुसार होगा:

aspnet_regiis -pe “appSettings” -app “/TestApp” -prov “DataProtectionConfigurationProvider”

Core ASP.NET WebForms in Hindi - BccFalna.com: TechTalks in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Core ASP.NET WebForms with C# in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

Core ASP.NET WebForms in Hindi | Page:647 | Format: PDF

BUY NOW GET DEMO REVIEWS