Tác giả: neverland87
Cấp độ bài viết: Vừa
Tóm tắt:
Trong lập trình web thì việc bảo mật thông tin luôn giữ một vai trò quan trọng. Khi làm việc với web asp.net 2.0 thì bạn phải thao tác với tập tin cấu hình web.config của nó. Đây là nơi chứa đựng những thông tin nhạy cảm. Và với vai trò là 1 nhà quản trị web, bạn phải tìm cách mã hóa (và tất nhiên là phải giải mã được) những đoạn (sections) trong tập tin này. Trong bài viết này, mình sẽ hướng dẫn bạn mã hóa phân đoạn <connectionStrings></connectionStrings>.
Bước 1: Tạo mới hoặc mở ứng dụng web asp.net (nếu đã có).
Bước 2: Tạo tập tin web.config (nếu chưa có)
Bước 3: Mở tập tin web.config, và thêm vào phân đoạn connectionStrings như sau:
Code: Select all
<configuration> <appSettings> <add key="var1" value="SomeValue"/> </appSettings> <connectionStrings> <add name="MyConnString" connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True;" /> </connectionStrings> <system.web>...</configuration>
Bước 4: Thêm vào trang web của bạn 2 button control: btnEncrypt và btnDecrypt. Mục đích: khi nhấn vào btnEncrypt thì sẽ mã hóa thông tin trong phân đoạn <connectionStrings> và btnDecrypt sẽ có nhiệm vụ giải mã thông tin đã được mã hóa.
Bước 5: Vào vùng soạn thảo code (nhấn F7), khai báo 2 biến toàn cục sau:
Code: Select all
Private provider As String = "RSAProtectedConfigurationProvider"Private section As String = "connectionStrings"
Bước 6: Thụ lý tình huống Click cho btnEncrypt:
Code: Select all
Protected Sub btnEncrypt_Click(ByVal sender As Object, ByVal e As EventArgs)Try Dim confg As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) Dim confStrSect As ConfigurationSection = confg.GetSection(section) If Not confStrSect Is Nothing Then confStrSect.SectionInformation.ProtectSection(provider) confg.Save() End If Response.Write("Configuration Section " & "<b>" & WebConfigurationManager.ConnectionStrings("MyConnString").ConnectionString & "</b>" & " is automatically decrypted")Catch ex As Exception End TryEnd Sub
Code: Select all
Protected Sub btnDecrypt_Click(ByVal sender As Object, ByVal e As EventArgs)Try Dim confg As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) Dim confStrSect As ConfigurationSection = confg.GetSection(section) If Not confStrSect Is Nothing AndAlso confStrSect.SectionInformation.IsProtected Then confStrSect.SectionInformation.UnprotectSection() confg.Save() End If Catch ex As Exception End TryEnd Sub
Do ứng dụng web được chạy tại hệ thống cục bộ, nên lúc này sẽ có 1 hộp thoại hiện ra cho biết tập tin web.config đã bị thay đổi nội dung, nó hỏi bạn có lưu hay không, bạn cứ chọn “Yes to All” là được.
Bây giờ bạn thử mở tập tin web.config lên, bạn sẽ thấy sự thay đổi rõ ràng ở phân đoạn connectionStrings:
Code: Select all
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue>CHuj8aWLgxN8oj1PfAREoZFoepWpnJoxVzejZuHiOiJcCm6xp9vJ2O9QthW6yaRcG+8l6GWc1gj9+h4qvgjMhsvUgCTBGhp8109rT5ZFCbPJ+cJY9p78zAFTS7luW3pFdTj2/PzL3/TDrs2jDAw9OvA0KAHxTtJVJgZZAiWxqvg=</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>Z+7AOlW/Xvk8vf6l+haaJytnUXfs8dFw1Lrpc9ovqrszJR1KsIL1zF1JW93CGy3UIHzQho/3DQhBJCgQpZXZs/pOyzUWe9wTX4SAOOZS2gsACHeZrDSYlaV1olJQC8Xjlq20FsomirRoL2jPGV2ypmMvR8yo+fx0Gpy43Qo8SRsN9K+oWbQsbicKo1btNCUAhYr4XMhlbonmMh2PjbjkDOxOumicuCaYiCXzvgzrj/k=</CipherValue> </CipherData> </EncryptedData> </connectionStrings>
Code: Select all
<connectionStrings> <add name="MyConnString" connectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True;" /> </connectionStrings>
Bước kế, ta tiến hành mã hóa phân đoạn vừa được trỏ tới qua đoạn code:
Code: Select all
confStrSect.SectionInformation.ProtectSection(provider)
Code: Select all
confg.Save()
Code: Select all
confStrSect.SectionInformation.UnprotectSection()