Sections
spotlight
     
     
WWWCoder.com Resource Directory

Asp.net Web.config Configuration File
2/19/2005 6:07:55 PM

Web.config file as it sounds like is a configuration file for the Asp.net web application. There is one web.config file for one asp.net application which configures the particular application. Web.config file is written in XML with specific tags having specific meanings. This article will cover the web.config and how it can affect your applications.

What is Web.Config File?

Web.config file as it sounds like is a configuration file for the Asp.net web application. There is one web.config file for one asp.net application which configures the particular application. Web.config file is written in XML with specific tags having specific meanings.

What is Machine.config File?

As web.config file is used to configure one asp.net web application, same way Machine.config file is used to configure the application according to a particular machine. Meaning that configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.

What can be stored in Web.config file?

There are number of important settings that can be stored in the configuration file. Here are some of the most important configurations.

1) Database connections

2) Session States

3) Error Handling

4) Security

Database Connections:

The most important thing to store in the web.config file is the database connection string. The reason of storing connection string in the web.config file makes sense since if later we ever want to change the location of our database we just have to change the connection string in the web.config file and thats it. This will certainly save us a lot of alteration in different files where we used the old connection.

Lets see a small example of the connection string which is stored in the web.config file.

<configuration>

  <appSettings>

     <add key="ConnectionString"

          value="server=localhost;uid=sa;pwd=;database=DBPerson" />

  </appSettings>

</configuration>

As you can see its really simple to store the connection string in the web.config file. The connection string is referenced by a key which in this case is "ConnectionString". The value attribute of the configuration file denotes the information about the database. Here we can see that if has database name, userid and password. You can define more options if you want.

There is a very good website that deals with all sorts of connection strings. Its called www.connectionstrings.com , in the website you will find the connection strings of all sorts of databases.

lets see how we access the connection string from our Asp.net web application.

using System.Configuration;

string connectionString = (string )ConfigurationSettings.AppSettings["ConnectionString"];

As you see its very simple to get the connection String out from the web.config and than use it in your application.

Session States:

Session in Asp.net web application is very important. As we know that HTTP is a stateless protocol and we needs session to keep the state alive. Asp.net stores the sessions in different ways. By default the session is stored in the asp.net process. You can always configure the application so that the session will be stored in one of the following ways.

1) Session State Service

There are two main advantages of using the State Service. First the state service is not running in the same process as the asp.net application. So even if the asp.net application crashes the sessions will not be destroyed. Any advantage is sharing the state information across a Web garden (Multiple processors for the same computer).

Lets see a small example of the Session State Service.

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:55455" sqlConnectionString="data source=127.0.0.1;user id=sa;password='' cookieless="false" timeout="20"

/>

The attributes are self explanatory but I will go over them.

mode: This can be StateServer or SqlServer. Since we are using StateServer we set the mode to StateServer.

stateConnectionString: connectionString that is used to locate the State Service. 

sqlConnectionString: The connection String of the sql server database.

cookieless: Cookieless equal to false means that we will be using cookies to store the session on the client side.  

 

2) SQL Server  

The final choice to save the session information is using the Sql Server 2000 database. To use Sql Server for storing session state you need to do the following:

1) Run the InstallSqlState.sql script on the Microsoft SQL Server where you intend to store the session.

You web.config settings will look something like this:

<sessionState mode = "SqlServer" stateConnectionString="tcpip=127.0.0.1:45565" sqlConnectionString="data source="SERVERNAME;user id=sa;password='' cookiesless="false" timeout="20"

/>

SQL Server lets you share session state among the processors in a Web garden or the servers in a Web farm. Apart from that you also get additional space to store the session. And after that you can take various actions on the session stored.

The downside is SQL Server is slow as compared to storing session in the state in process. And also SQL Server cost too much for a small company.

Error Handling: 

Error handling is one of the most important part of any web application. Each error has to be caught and suitable action has to be taken to resolve that problem. Asp.net web.config file lets us configure, what to do when an error occurs in our application.

Check the following xml tag in the web.config file that deals with errors:

<customErrors mode = "On">

<error statusCode = "404" redirect = "errorPage.aspx" />

</customErrors>

This tells the Asp.net to display custom errors from a remote client or a local client and to display a page named errorPage.aspx. Error "404" is "Page not found" error.

If custom error mode is turned "off" than you will see Asp.net default error message. This error messages are good for debugging purposes but should never be exposed to the users. The users should always be presented with friendly errors if any.

Security:

The most critical aspect of any application is the security. Asp.net offers many different types of security method which can be used depending upon the condition and type of security you need.

1) No Authentication: 

No Authentication means "No Authentication" :) , meaning that Asp.net will not implement any type of security.

2) Windows Authentication:

The Windows authentication allows us to use the windows user accounts. This provider uses IIS to perform the actual authentication, and then passes the authenticated identity to your code. If you like to see that what windows user is using the Asp.net application you can use:

User.Identity.Name;

This returns the DOMAIN\UserName of the current user of the local machine.

3) Passport Authentication:

Passport Authentication provider uses Microsoft's Passport service to authenticate users. You need to purchase this service in order to use it.

4) Forms Authentication:

Forms Authentication uses HTML forms to collect the user information and than it takes required actions on those HTML collected values.

In order to use Forms Authentication you must set the Anonymous Access checkbox checked. Now we need that whenever user tries to run the application he/she will be redirected to the login page.

<authentication mode="Forms">

<forms loginUrl = "frmLogin.aspx" name="3345C" timeout="1"/>

</authentication>

<authorization>

<deny users="?" />

</authorization>

As you can see we set the Authentication mode to "Forms". The forms loginUrl is the first page being displayed when the application is run by any user.

The authorization tags has the deny users element which contains "?", this means that full access will be given to the authenticated users and none access will be given to the unauthenticated users. You can replace "?" with "*" meaning that all access is given to all the users no matter what.

Final Words: 

As you have seen that Web.config file plays a very important role in the over all Asp.net application. There are alot more features that I have not discussed which includes caching. Try using web.config file when you need to configure the overall application.

About the Author:

Mohammad Azam, also known as Azamsharp have been programming in .NET for 4 years. He is the author of several articles which can be viewed on his website www.azamsharp.cjb.net . Apart from the articles Azamsharp is also the Top 50 poster on Microsoft official forums (www.asp.net )

At present Azamsharp is completing his undergraduate degree in Computer Science from University of Houston and also working as a .NET consultant for cSoft Technologies.

You can reach Azamsharp at xMohammadAzamx@yahoo.com


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 3/17/2010 7:00:19 AM by Anonymous
Comments: fine
Left on 2/20/2010 12:23:05 AM by Anonymous
Comments: <configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

No ratings available.
Left on 12/29/2009 3:47:10 AM by Anonymous
Comments: <customErrors
No ratings available.
Left on 12/3/2009 6:48:51 AM by Anonymous
Comments: Nice article
Left on 11/13/2009 2:35:27 AM by Anonymous
Comments: <!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>
Left on 9/20/2009 7:42:57 AM by Anonymous
Comments: Nice article. But for realy rapid editing web.config file I'm use my own visual tool: http://sites.google.com/site/webconfigtool/
Left on 6/19/2009 7:09:26 AM by Anonymous
Comments: Very Nice Info
Left on 6/2/2009 7:19:13 PM by Anonymous
Comments: Helpful for beginner's
Left on 5/7/2009 3:31:01 AM by Anonymous
Comments: very fine
No ratings available.
Left on 4/1/2009 5:48:30 AM by Anonymous
Comments: good explanation and simple
Left on 1/12/2009 10:14:14 PM by Anonymous
Comments: it`s really help ful for beginners
Left on 9/16/2008 2:12:04 AM by Anonymous
Comments: insufficient
No ratings available.
Left on 9/16/2008 2:11:30 AM by Anonymous
Comments: did not get satisfied
No ratings available.
Left on 9/8/2008 4:28:07 AM by Anonymous
Comments: I got more from this article
Left on 8/28/2008 5:49:51 AM by Anonymous
Comments: It is realy helpfull to every one Gr8t Work ..........

Left on 6/21/2008 2:22:33 AM by Anonymous
Comments: very nice one
Left on 4/9/2008 8:14:54 AM by Anonymous
Comments: Gr8t Work
Left on 1/25/2008 4:17:51 AM by Anonymous
Comments: so simple so nicely
Left on 10/15/2007 7:18:48 AM by Anonymous
Comments: Nice One ...
Left on 8/9/2007 9:33:34 AM by Anonymous
Comments: you tell nothing that no one else tells.  where do we put this single lines?  not just anywhere.
Left on 6/27/2007 1:18:36 AM by Anonymous
Comments: Excellet, i satisfection it
Left on 6/5/2007 6:03:20 PM by Anonymous
Comments: HOW DO U GO TO MYSPACE HOME PAGE

Left on 6/1/2007 6:09:10 AM by Anonymous
Comments: great
Left on 2/15/2007 4:59:16 AM by Anonymous
Comments: Nice
Left on 9/23/2006 2:39:03 AM by Anonymous
Comments:
Left on 5/22/2006 11:00:13 AM by Anonymous
Comments: pethatic.No good information toget.how can your write all such stuff.
No ratings available.
Left on 5/1/2006 1:44:19 AM by Anonymous
Comments: good & helpful
No ratings available.
Left on 4/24/2006 2:11:13 AM by Anonymous
Comments: ok...ravindra..mumbai
No ratings available.
Left on 4/21/2006 2:18:25 AM by Anonymous
Comments: I hate you
Left on 4/18/2006 1:26:14 AM by Anonymous
Comments: Really good! Would appreciate storing encoded password also included in examples
Left on 4/14/2006 12:30:38 PM by Anonymous
Comments: easy 2 understand 4 beginners......gr8 stuff
Left on 4/7/2006 1:36:45 AM by Anonymous
Comments: Excellent

Left on 4/6/2006 7:44:12 AM by Anonymous
Comments: This content is really easy to understand.Keep up the good work guys.

Arun(Chandigarh)
No ratings available.
Left on 4/5/2006 2:58:11 AM by Anonymous
Comments: This is really a very goos Article. But explain more like the possible namespaces can be used in web.config. Thanx a lot for this.
Abhishek Saxena (INDIA)
No ratings available.
Left on 4/4/2006 7:09:26 AM by Anonymous
Comments: hi
No ratings available.
Left on 3/28/2006 8:26:15 PM by Anonymous
Comments: luv it

Left on 3/26/2006 11:51:35 PM by Anonymous
Comments: its to good
Left on 3/22/2006 4:22:55 AM by Anonymous
Comments: Best he g
No ratings available.
Left on 3/21/2006 4:48:43 AM by Anonymous
Comments: good one
Left on 3/1/2006 2:57:38 AM by Anonymous
Comments: very good
No ratings available.
Left on 2/25/2006 7:49:10 AM by Anonymous
Comments: web deugging server is not start
No ratings available.
Left on 2/21/2006 6:55:37 AM by Anonymous
Comments: Excellent one
Left on 2/20/2006 3:27:34 AM by Anonymous
Comments: good short description
Left on 1/27/2006 7:23:01 AM by Anonymous
Comments: it Help me and solve my problem.Can you please explain me web.config of dotnetnuke each tag.please mail me at modgray@hotmail.com
Left on 1/22/2006 5:26:05 AM by Anonymous
Comments: Very informative.
Helpful for beginners.

Left on 1/19/2006 12:32:24 AM by Anonymous
Comments: yeh kya hua
No ratings available.
Left on 8/18/2005 2:56:21 AM by Anonymous
Comments: Simple n sober...fully upto the mark
Left on 8/8/2005 12:46:51 PM by Anonymous
Comments: For the guy who said connections string in web config is poor programming:
You moron! web.configs are compiled. MS would not have included a connection string tag if it was bad.
Left on 7/6/2005 10:45:25 AM by Anonymous
Comments: this is what i was searching for

Left on 6/10/2005 5:32:02 AM by Anonymous
Comments: There is a Problem for storing Session Data in Web.Config file.
It says that Unrecognised Configuration Section.
No ratings available.
Left on 6/5/2005 1:59:18 PM by Anonymous
Comments: It was really excellent
No ratings available.
Left on 5/5/2005 2:27:47 AM by Anonymous
Comments: Very good doc for the beginner
Left on 4/29/2005 12:25:10 PM by Anonymous
Comments: Web.Config Configuration File --> <configuration> <system.web> <customErrors mode="Off"/> </system.web> </configuration
No ratings available.
Left on 4/25/2005 3:25:49 AM by Anonymous
Comments: Good Example
Left on 4/19/2005 2:57:32 PM by Anonymous
Comments: A lot of help
Left on 4/13/2005 2:57:12 PM by Anonymous
Comments:


   
       
   

Left on 4/13/2005 2:55:49 PM by Anonymous
Comments:


   
       
   






   
       
   


No ratings available.
Left on 4/4/2005 3:21:29 PM by Anonymous
Comments: So, are there any security threats by storing information in the web.config?  Is there anyway unauthorized users can ever access the file, and then be able to steal information from it ... including the DB connection string?
No ratings available.
Left on 4/1/2005 4:32:58 PM by Anonymous
Comments: I have poop on my shorts
Left on 3/29/2005 12:54:28 AM by Anonymous
Comments: good stuff
No ratings available.
Left on 3/24/2005 5:13:15 PM by Anonymous
Comments: Real good stuff for beginners!
Left on 3/23/2005 10:15:46 AM by Anonymous
Comments: it is very useful to developers
No ratings available.
Left on 3/23/2005 8:32:19 AM by Anonymous
Comments: Its makes a sence about web.config file, the language is too good.
No ratings available.
Left on 3/21/2005 10:19:46 AM by Anonymous
Comments: my name is vijay ,u r article is superb
No ratings available.
Left on 3/21/2005 12:28:15 AM by Anonymous
Comments: i like it
Left on 3/16/2005 1:54:57 PM by Anonymous
Comments: Same comment as one below:  I do not understand a word of this. It didn't help me out one bit.
Left on 3/16/2005 1:31:59 PM by Anonymous
Comments: I do not understand a word of this. It didn't help me out one bit.
Left on 3/15/2005 11:17:29 PM by Anonymous
Comments: Overall the article was very good. Easy to understand. I dont agree with "poor" english. This is not about learning english, this is about learning asp.net.
Left on 3/15/2005 9:43:01 AM by Anonymous
Comments: Info is good, but this guy needs a proofreader.  The poor english makes reading and understanding difficult.
Left on 3/13/2005 4:42:41 PM by Anonymous
Comments: cookieless: Cookieless equal to false means that we will be using cookies to store the session on the client side. 

Setting cookieless=false, does not store the session on the client side, only the session identifier is stored in the cookies, not the entire session data
No ratings available.
Left on 3/11/2005 3:57:11 PM by Anonymous
Comments: I would like to include that web.config file is not deployment with the asp.net application. So, its particulary safe to store the connection string. But good practice will be to encrypt it.
Left on 3/11/2005 3:54:29 PM by Anonymous
Comments: Good for begginers :D
Left on 3/10/2005 6:59:00 AM by Anonymous
Comments: custem error
No ratings available.
Left on 3/3/2005 4:25:45 AM by Anonymous
Comments: Very Bad
No ratings available.
Left on 3/3/2005 4:20:06 AM by Anonymous
Comments: difficult for my level to understand.
No ratings available.
Left on 3/2/2005 4:23:41 PM by Anonymous
Comments: Note  You can use Web.config or Machine.config to store encrypted connection strings. Machine.config is preferred as it is in a system directory outside of a virtual directory. This is discussed further in the next section, "Using Web.config and Machine.config."
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetch12.asp
Left on 3/2/2005 4:18:50 PM by Anonymous
Comments: OMG storing the connection string in the Web.Config is against microsoft best practices.  DO SOME RESEARCH WOULD YOU!
Left on 3/2/2005 4:15:14 AM by Anonymous
Comments: the resource provided is very much helpful to gain some knowledge of session,error handling and types of authentication
Left on 2/25/2005 1:01:07 AM by Anonymous
Comments: succinent!
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use