Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication

The registry represents one possible location for an application to store database connection strings. Although individual registry keys can be secured with Windows access control lists (ACLs), for added security you should store encrypted connection strings.

This How To describes how to store an encrypted database connection string in the registry and retrieve it from an ASP.NET Web application. It uses the generic encryption and decryption managed class library created in How to: Create an Encryption Library, which can be found in Reference section of this guide.

If you have not already created the encryption class library assembly, do so before continuing with the current How To.

For more information about other locations and ways of securely storing database connection strings, see Storing Database Connection Strings Securely in Chapter 12, "Data Access Security."

Notes

  • The connection string, initialization vector and key used for encryption will be stored in the registry as named values beneath the following registry key.
    HKEY_LOCAL_MACHINE/Software/TestApplication
    
  • The initialization vector and key must be stored in order to allow the connection string to be decrypted.

Requirements

The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs you will need.

  • Microsoft?Windows?2000 operating system
  • Microsoft Visual Studio?.NET development system

The procedures in this article also require that you have knowledge of the Microsoft Visual C#?development tool.

Summary

This How To includes the following procedures:

  1. Store the Encrypted Data in the Registry
  2. Create an ASP.NET Web Application

1. Store the Encrypted Data in the Registry

This procedure creates a Windows application that will be used to encrypt a sample database string and store it in the registry.

To store the encrypted data in the registry

  1. Start Visual Studio .NET and create a new C# Windows project called EncryptionTestApp.
  2. Add an assembly reference to the Encryption.dll assembly.

    To create this assembly, you must perform the steps described in How To: Create an Encryption Library in the Reference section of this guide.

  3. Add the following using statements to the top of Form1.cs beneath the existing using statements.
    using Encryption;
    using System.Text;
    using Microsoft.Win32;
    
  4. Add the controls in Table 1 to Form1 and arrange them as illustrated in Figure 1.

    Table 1. EncryptionTestApp controls

    ControlTextID
    LabelConnection String: 
    TextBox txtConnectionString
    LabelKey: 
    TextBox txtKey
    LabelInitialization Vector: 
    TextBox txtInitializationVector
    LabelEncrypted String 
    TextBox txtEncryptedString
    LabelDecrypted String 
    TextBox txtDecryptedString
    ButtonEncryptbtnEncrypt
    ButtonDecryptbtnDecrypt
    ButtonWrite Registry DatabtnWriteRegistryData

    Figure 1. Encryption Test Harness dialog box

  5. Set the Text property of txtConnectionString to
    "Server=local; database=pubs; uid=Bob; pwd=Password"
    
  6. Set the Text property of txtKey to
    "0123456789012345"
    

    The key length is 16 bytes to suite the Triple DES encryption algorithm.

  7. Set the Text property of Form1 to
    "Encryption Test Harness"
    
  8. Double-click the Encrypt button to create a button click event handler and add the following code to the event handler.
    try
    {
      // Create the encryptor object, specifying 3DES as the
      // encryption algorithm
      Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes);
      // Get the connection string as a byte array
      byte[] plainText = 
        Encoding.ASCII.GetBytes(txtConnectionString.Text);
      byte[] key = Encoding.ASCII.GetBytes(txtKey.Text);
    
      // Perform the encryption
      byte[] cipherText = enc.Encrypt(plainText, key);
      // Store the intialization vector, as this will be required
      // for decryption
      txtInitializationVector.Text = Encoding.ASCII.GetString(enc.IV);
    
      // Display the encrypted string
      txtEncryptedString.Text = Convert.ToBase64String(cipherText);
    }
    catch(Exception ex)
    {
      MessageBox.Show("Exception encrypting: " + ex.Message, 
                      "Encryption Test  Harness");
    }
    
  9. Return to Form1 in Designer mode and double-click the Decrypt button to create a button click event handler.
  10. Add the following code to the Decrypt button event handler.
    try
    {
      // Set up the Decryptor object
      Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes);
    
      // Set the Initialization Vector
      dec.IV = Encoding.ASCII.GetBytes(txtInitializationVector.Text);
    
      byte[] key = Encoding.ASCII.GetBytes(txtKey.Text);
      // Perform the decryption
      byte[] plainText =  dec.Decrypt(Convert.FromBase64String(
                                      txtEncryptedString.Text),
                                      key);
    
      // Display the decrypted string.
      txtDecryptedString.Text = Encoding.ASCII.GetString(plainText);
    }
    catch(Exception ex)
    {
      MessageBox.Show("Exception decrypting. " + ex.Message, 
                      "Encryption Test Harness");
    }
    
  11. Return to Form1 in Designer mode and double-click the Write Registry Data button to create a button click event handler.
  12. Add the following code to the event handler.
    // Create registry key and named values
    RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software",true);
    rk = rk.CreateSubKey("TestApplication");
    
    // Write encrypted string, initialization vector and key to the 
      registry
    rk.SetValue("connectionString",txtEncryptedString.Text);
    rk.SetValue("initVector",Convert.ToBase64String(
               Encoding.ASCII.GetBytes(txtInitializationVector.Text)));
    rk.SetValue("key",Convert.ToBase64String(Encoding.ASCII.GetBytes(
                                             txtKey.Text)));
    MessageBox.Show("The data has been successfully written to the 
      registry");
    
  13. Run the application, and then click Encrypt.

    The encrypted connection string is displayed in the Encrypted String field.

  14. Click Decrypt.

    The original string is displayed in the Decrypted String field.

  15. Click Write Registry Data.
  16. In the message box, click OK.
  17. Run regedit.exe and view the contents of the following key.
    HKLM/Software/TestApplication
    

    Confirm that encoded values are present for the connectionString, initVector and key named values.

  18. Close regedit and the test harness application.

2. Create an ASP.NET Web Application

This procedure develops a simple ASP.NET Web application that will retrieve the encrypted connection string from the registry and decrypt it.

To create an ASP.NET application

  1. Create a new Visual C# ASP.NET Web Application called EncryptionWebApp.
  2. Add an assembly reference to the Encryption.dll assembly.

    To create this assembly, you must perform the steps described in How To: Create an Encryption Library in the Reference section of this guide.

  3. Open Webform1.aspx.cs and add the following using statements at the top of the file beneath the existing using statements.
    using Encryption;
    using System.Text;
    using Microsoft.Win32;
    
  4. Add the controls listed in Table 2 to WebForm1.aspx.

    Table 2: WebForm1.aspx controls

    ControlTextID
    Label lblEncryptedString
    Label lblDecryptedString
    ButtonGet Connection StringbtnGetConnectionString

  5. Double-click the Get Connection String button to create a button click event handler.
  6. Add the following code to the event handler.
    RegistryKey rk = Registry.LocalMachine.OpenSubKey(
                                    @"Software/TestApplication",false);
    lblEncryptedString.Text = (string)rk.GetValue("connectionString");
    
    string initVector = (string)rk.GetValue("initVector");
    string strKey = (string)rk.GetValue("key");
    
    Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes );
    dec.IV = Convert.FromBase64String(initVector);
    
    // Decrypt the string
    byte[] plainText = dec.Decrypt(Convert.FromBase64String(
                                   lblEncryptedString.Text), 
                                   Convert.FromBase64String(strKey));
    
    lblDecryptedString.Text = Encoding.ASCII.GetString(plainText);
    
  7. On the Build menu, click Build Solution.
  8. Right-click Webform1.aspx in Solution Explorer, and then click View in Browser.
  9. Click Get Connection String.

    The encrypted and decrypted connection strings are displayed on the Web form.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当你看到ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.的错误信息时,它表示MySQL数据库配置了caching_sha2_password认证插件,并要求使用安全连接进行身份验证。这个错误通常发生在尝试连接到MySQL服务器时,但没有使用加密连接。为了解决这个问题,你可以采取以下步骤: 1. 确保你的MySQL客户端和服务器都支持SSL/TLS加密。这可以通过检查它们的版本和配置来确认。 2. 在连接MySQL服务器时,确保使用了安全连接。这意味着你需要使用SSL/TLS协议来加密通信。你可以在连接字符串或命令行选项中指定使用SSL/TLS。 3. 如果你正在使用命令行客户端,可以在连接MySQL服务器时使用--ssl选项来启用安全连接。例如,mysql --ssl=1 -u username -p。 4. 如果你使用的是应用程序或框架,查看它们的文档以了解如何配置和启用安全连接。 总之,当你遇到ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.的错误时,你需要确认你的MySQL客户端和服务器都支持SSL/TLS加密,并使用安全连接来连接MySQL服务器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ERROR 2061 (HY000): Authentication plugin ‘caching_sha2_password‘ reported error: Authentication](https://blog.csdn.net/hezuijiudexiaobai/article/details/131258873)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [主从复制报错2061:Authentication plugin 'caching_sha2_password' reported error:Authentication ...](https://blog.csdn.net/weixin_30647423/article/details/114816675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denal

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值