Application Domains in Asp.Net

When we launch the Notepad program in Windows, the program executes inside of a container known as a process. We can launch multiple instances of Notepad, and each instance will run in a dedicated process. Using the Task Manager application, we can see a list of all processes currently executing in the system.

A process contains the executable code and data of a program inside memory it has reserved from the operating system. There will be at least one thread executing instructions inside of the process, and in most cases there are multiple threads. If the program opens any files or other resources, those resources will belong to the process.

A process is also boundary. Erroneous code inside of a process cannot corrupt areas outside of the current process. It is easy to communicate inside of a process, but special techniques are required to communicate from one process to another. Each process also runs under a specific security context which can dictate what the process can do on the machine and network.  

A process is the smallest unit of isolation available on the Windows operating system. This could pose a problem for an ISP who wants to host hundreds of ASP.NET applications on a single server. The ISP will want to isolate each ASP.NET application to prevent one application from interfering with another company’s application on the same server, but the relative cost of launching and executing a process for hundreds of applications may be prohibitive.

 

Introducing the Application Domain

 

.NET introduces the concept of an application domain, or AppDomain. Like a process, the AppDomain is both a container and a boundary. The .NET runtime uses an AppDomain as a container for code and data, just like the operating system uses a process as a container for code and data. As the operating system uses a process to isolate misbehaving code, the .NET runtime uses an AppDomain to isolate code inside of a secure boundary.

Note, however, that the application domain is not a secure boundary when the application runs with full trust. Applications running with full trust can execute native code and circumvent all security checks by the .NET runtime. ASP.NET applications run with full trust by default.

An AppDomain belongs to only a single process, but single process can hold multiple AppDomains. An AppDomain is relatively cheap to create (compared to a process), and has relatively less overhead to maintain than a process. For these reasons, an AppDomain is a great solution for the ISP who is hosting hundreds of applications. Each application can exist inside an isolated AppDomain, and many of these AppDomains can exist inside of a single process – a cost savings.

 

AppDomains And You

 

You’ve created two ASP.NET applications on the same server, and have not done any special configuration. What is happening?

A single ASP.NET worker process will host both of the ASP.NET applications. On Windows XP and Windows 2000 this process is named aspnet_wp.exe, and the process runs under the security context of the local ASPNET account. On Windows 2003 the worker process has the name w3wp.exe and runs under the NETWORK SERVICE account by default.

An object lives in one AppDomain. Each ASP.NET application will have it’s own set of global variables: Cache, Application, and Session objects are not shared. Even though the code for both of the applications resides inside the same process, the unit of isolation is the .NET AppDomain. If there are classes with shared or static members, and those classes exist in both applications, each AppDomain will have it’s own copy of the static fields – the data is not shared. The code and data for each application is safely isolated and inside of a boundary provided by the AppDomain

In order to communicate or pass objects between AppDomains, you’ll need to look at techniques in .NET for communication across boundaries, such as .NET remoting or web services.

Note again: the one caveat to the idea of an AppDomain as a boundary is that ASP.NET applications will run with full trust by default. Fully trusted code can execute native code, and native code can essentially have access to anything inside the process. You’ll need to run applications with partial trust to restrict access to unmanged code and verify all managed code to secure AppDomains.

 

Shadow Copies and Restarts

 

Once an assembly is loaded into an AppDomain, there is no way to remove the assembly from the AppDomain. It is possible, however, to remove an AppDomain from a process.

If you copy an updated dll into an application’s bin subdirectory, the ASP.NET runtime recognizes there is new code to execute. Since ASP.NET cannot swap the dll into the existing AppDomain , it starts a new AppDomain. The old application domain is “drain stopped”, that is, existing requests are allowed to finish executing, and once they are all finished the AppDomain can unload. The new AppDomain starts with the new code and begins taking all new requests.

Typically, when a dll loads into a process, the process locks the dll and you cannot overwrite the file on disk. However, AppDomains have a feature known as Shadow Copy that allows assemblies to remain unlocked and replaceable on disk.

The runtime initializes ASP.NET with Shadow Copy enabled for the bin directory. The AppDomain will copy any dll it needs from the bin directory to a temporary location before locking and loading the dll into memory. Shadow Copy allows us to overwrite any dll in the bin directory during an update without taking the web application offline.

 

Master Of Your Domain

Application domains replace the OS process as the unit of isolation for .NET code. An understanding of application domains will give you an idea of the work taking place behind the scenes of an ASP.NET application. Using the CurrentDomain property of the AppDomain class you can inspect properties about the AppDomain your code is executing in, including the Shadow Copy settings we discussed in this article.

Protected: Web Services Basics

This post is password protected. To view it please enter your password below:


Protected: C# at Fast Track

This post is password protected. To view it please enter your password below:


Protected: Asp.net at Fast Track

This post is password protected. To view it please enter your password below:


Protected: WWF at Fast Track

This post is password protected. To view it please enter your password below:


Protected: WCF at Fast Track

This post is password protected. To view it please enter your password below:


Sample DotNet

Protected: Interview Help

This post is password protected. To view it please enter your password below:


Protected: Interview Guide

This post is password protected. To view it please enter your password below:


Locking a folder in Windows XP

Since some people were having issues with this method of hiding a folder in XP (folder being renamed incorrectly, visible via the command prompt, etc), I am adding to the bottom another way to permanently hide a folder using a program called Free Hide Folder. Please scroll down to read about it.

If you’ve come across this page looking for a way to create a password protected, secure, hidden or locked folder in Windows XP for free, you’ve come to the right place. Unfortunately, Windows XP does not have any user-friendly built-in tools to create secure folders on computers with multiple users. Of course, you can encrypt data on your hard disk, but this only helps if your computer is stolen.

In a previous post, I wrote about how you can hide a folder in Windows XP using a rudimentary built-in feature. This method is useful if you want to hide a folder or file and you’re pretty sure that anyone who is using the computer will have no idea how to un-hide the data. If you’re dealing with someone a little more computer savvy, there is another way to create a secure locked folder without having to buy any 3rd party software.

It basically involves using a script to morph the folder into something completely different so that it’s still visible, but nothing inside the folder is accessible. In order to view the data, you have to run another script to morph it back. Anyone who clicks on the folder will be brought to the Control Panel and simply think it is a shortcut.

Here are the steps to create the protected folder in Windows XP:

  • First create a folder that you will use to store your confidential data. For example, I have created a folder called Fonts at the root of my D drive. Since the hidden folder will bring you to the Control Panel, it’s best to name the folder as one of the programs inside there.

createsecurefolder1

  • In the same location where you created the new folder, create a new file in Notepad, copy the following below into it, replace Fonts with the name of your folder and save it as loc.bat.

ren Fonts Fonts.{21EC2020-3AEA-1069-A2DD-08002B30309D}

  • To save the file as a .bat file in Notepad, just put the whole thing in quotes, like “loc.bat” and then click Save.

hiddenfolder

  • Now you should have a bat file named loc in the same directory as your Fonts folder, not inside the Fonts folder.

lockedfolders1

  • Create another NotePad file and type in the following listed below and save it as “key.bat”.

ren Fonts.{21EC2020-3AEA-1069-A2DD-08002B30309D} Fonts

  • Now you’ll have loc.bat and key.bat along with your folder. Go ahead and double-click on loc.bat and your folder will turn into the Control Panel and nothing inside can be viewed. Clicking on it will simply bring you to the Control Panel. You’ll notice the icon should have changed also.

protectedfolders

  • To view the data inside your folder again, click on key.bat and your folder will return to normal! Pretty simple!

Of course, keeping the key.bat file in the same folder will defeat the purpose of securing the folder, so it’s best to move the key.bat file somewhere else, or even better, put it on a USB stick or CD that only you can access.

Most people will be probably look at the folder and simply ignore it since it has the control panel icon and it links directly there. Of course, if someone knows this trick also, they can create their own key.bat file, etc and open it back up. However, if you’re dealing with someone who can do all of that, it’s best you check out more advanced 3rd party software to lock down a folder.

ALTERNATIVE METHOD:

If you’re having trouble with the above-mentioned method (from what I can see from the comments, a good number of people), then here’s another quick and free way to create a secure folder in Windows XP. There is a very nice program called Free Hide Folder from Cleanersoft that allows you to easily hide your private folders.

Your folders are completely hidden and you must supply a password to open the program to unhide the folders. The personal edition is free, just click the Skip Registration button unless you’re using it for commercial purposes.

free hide folder

I’ve tried this program myself and I cannot access the hidden folder via Windows Explorer nor the command prompt. So if the above method is not working, give this a shot!

Follow

Get every new post delivered to your Inbox.