分享
 
 
 

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

王朝asp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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:

Store the Encrypted Data in the Registry

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

Start Visual Studio .NET and create a new C# Windows project called EncryptionTestApp.

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.

Add the following using statements to the top of Form1.cs beneath the existing using statements. using Encryption;

using System.Text;

using Microsoft.Win32;

Add the controls in Table 1 to Form1 and arrange them as illustrated in Figure 1.

Table 1. EncryptionTestApp controls

Control

Text

ID

Label

Connection String:

TextBox

txtConnectionString

Label

Key:

TextBox

txtKey

Label

Initialization Vector:

TextBox

txtInitializationVector

Label

Encrypted String

TextBox

txtEncryptedString

Label

Decrypted String

TextBox

txtDecryptedString

Button

Encrypt

btnEncrypt

Button

Decrypt

btnDecrypt

Button

Write Registry Data

btnWriteRegistryData

Figure 1. Encryption Test Harness dialog box

Set the Text property of txtConnectionString to "Server=local; database=pubs; uid=Bob; pwd=Password"

Set the Text property of txtKey to "0123456789012345"

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

Set the Text property of Form1 to "Encryption Test Harness"

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");

}

Return to Form1 in Designer mode and double-click the Decrypt button to create a button click event handler.

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");

}

Return to Form1 in Designer mode and double-click the Write Registry Data button to create a button click event handler.

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");

Run the application, and then click Encrypt.

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

Click Decrypt.

The original string is displayed in the Decrypted String field.

Click Write Registry Data.

In the message box, click OK.

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.

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

Create a new Visual C# ASP.NET Web Application called EncryptionWebApp.

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.

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;

Add the controls listed in Table 2 to WebForm1.aspx.

Table 2: WebForm1.aspx controls

Control

Text

ID

Label

lblEncryptedString

Label

lblDecryptedString

Button

Get Connection String

btnGetConnectionString

Double-click the Get Connection String button to create a button click event handler.

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);

On the Build menu, click Build Solution.

Right-click Webform1.aspx in Solution Explorer, and then click View in Browser.

Click Get Connection String.

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有