Wednesday, May 28, 2008

How to create DLL for .NET 2.0 Web Application

How to create DLL for .NET 2.0 Web Application

If you have layers structure in .NET solution and when you build the solution, visual studio creates DLLs for business layer and data access layer, but visual studio not creates DLL for presentation or UI layer.

You will face issue to run Microsoft FxCop tool for Presentation layer. To run FxCop tool you needs dll or exe. You can create dll for presentation layer using VS 2005, follow the steps below

1) Create a new folder on C drive, like: C:\Temp\ProjectDeploy

2) Open the project in the Visual studion 2005

3) Go to the project right click and choose “Publish Web site” option

4) Give “C:\Temp\ProjectDeploy” in the Target Location and click “OK”

5) You will see your presentation layer dlls inside the ProjectDeploy\bin folder

Note: Fxcop Tool can be integrate with visual studio 2005 see more details here

Tuesday, May 13, 2008

PROGRAMATICALLY CONFIGURATION FILES ENCRYPTION AND DECRYPTION USING C#.NET

PROGRAMATICALLY CONFIGURATION FILES ENCRYPTION AND DECRYPTION USING C#.NET

In .Net application (web/window) configuration file we used to make application setting as configurable. Each and every time you need not to compile your project for any setting changes in the configuration file. This configuration file present in virtual directory of the application. When you deploy the application, it can be editable by the users who have all permission for that deployment server and one server can have several applications, this time it is necessary to keep your application configuration file secure or use encrypted configuration file to secure your application setting data.

Previously we used to write encryption decryption function using different .Net notation (like: SHA, MD5 etc) and manually updating configuration file. Here I am giving new way to do same task programmatically, you can do encrypt or decrypt any project configuration file appSettings or connectionStrings sections programmatically

ASP.NET 2.0 and above makes it extremely easy to encrypt connection strings, encrypt application settings, and encrypt config sections in Web.config either via the command prompt with aspnet_regiis or programmatically in your web applications.

Source Code:

Create application to encrypt or decrypt configuration file using C#/VB.Net windows or web application.

Here I am creating window application using C#.net, Follow the simple four steps to create application.

1) app.config file sections Before Encryption

For example:

<appSettings>

<add key="ServerName" value="Your Machine Name" />

<add key="Password" value="123.456.78" />

</appSettings>

2) app.config file sections After Encryption

For example:

<appSettings configProtectionProvider="DataProtectionConfigurationProvider">

<EncryptedData>

<CipherData>

<CipherValue>MSDFSDFGSSD$SFSD%VAAAAAAGDFGDFGGGGGGGVBCXBVBVCBBVCVBVCBBBTYRTYRTY%UUUUUUUU</CipherValue>

</CipherData>

</EncryptedData>

</appSettings>

2) You can design your window form something like below

3) Code for Encrypt and Decrypt button

NameSpace

using System.Web.Configuration;

Call following function from Encrypt button code behind

Function for Encryption

private int EncryptConfigurationSection(string fileName, string sectionName, string provider)

{

//Creates a FileMap Object to store the File Name of Configuration File

ExeConfigurationFileMap FileMap = new ExeConfigurationFileMap();

//Assigning the File Name to the File Map

FileMap.ExeConfigFilename = fileName;

//Retrieving the Configuration from the File Provided

Configuration config =

ConfigurationManager.OpenMappedExeConfiguration(FileMap, ConfigurationUserLevel.None);

//Checking if the File Provided is a Configuration File Or Not

if (config.HasFile) {

//Retrieve the Section from the Configuration Object

ConfigurationSection section = config.GetSection(sectionName);

//Check if the Section is not null or is not Previously Protected

if (section != null && !section.SectionInformation.IsProtected) {

//Provide Protection to the Section as per the provider

section.SectionInformation.ProtectSection(provider);

//Save the Configuration object and the File

config.Save();

return 1;

}

else{

if (section != null) {return 3;}

else {return 0;}

}

}

else {return 2;}

}

Function for De-Encryption

Call following function from Dncrypt button code behind

private int DncryptConfigurationSection(string fileName, string sectionName)

{

//Creates a FileMap Object to store the File Name of Configuration File

ExeConfigurationFileMap FileMap = new ExeConfigurationFileMap();

//Assigning the File Name to the File Map

FileMap.ExeConfigFilename = fileName;

//Retrieving the Configuration from the File Provided

Configuration config =

ConfigurationManager.OpenMappedExeConfiguration(FileMap, ConfigurationUserLevel.None);

//Checking if the File Provided is a Configuration File Or Not

if (config.HasFile) {

//Retrieve the Section from the Configuration Object

ConfigurationSection section = config.GetSection(sectionName);

//Check if the Section is not null or is not Previously Protected

if (section != null && section.SectionInformation.IsProtected) {

//Remove the Protection from the Section

section.SectionInformation.UnprotectSection();

//Save the Configuration Object and the File

config.Save();

return 1;

}

else {

if (section != null) {return 3;}

else {return 0;}

}

} else {return 2;}

}

Note:

In the above functions 0 to 3 used for

0 --> Wrong Section as per Configuration file

1 --> Successful Encyprtion/Decryption Information

2 --> Wrong Configuration File name

3 --> Configuration section in file is not encrypted

H/W Platform: Dual Processor with 1 GB RAM

S/W Environment: ASP.NET, VB.NET and C#.NET