Web.Config |
Written on: Nov, 16th 2001. |
Application("DSN") = "Server=moon; Driver=Sql Server; Database=Store; UID=user; PWD=bingo;"
Above declaration in the global.asa file might be familiar to almost all ASP programmers.
While going through the MSDN, I was overwhelmed, by looking into the web.config file which
handles all configuration for an application. The replacement for the above declaration
in ASP .NET is as follows:
<configuration>
<appSettings>
<add key="DSN" value="Server=moon;database=Store;Trusted_Connection=yes" />
</appSettings>
</configuration>
Then, in your ASPX page, you should have the following statement to retrieve the value
for DSN.
Dim dsn As String = ConfigurationSettings.AppSettings("DSN")
So, I started to ask the following questions to myself.
What exactly is web.config?
Does this handles only the above example?
What are the benefits of web.config?
And, following were the results for my questions, and I would like to share with you all.
This is based on Beta2
Introduction
Well, web.config is a XML-based configuration file. If you see the above example, you can
make sure that all the elements are based on XML standards. Obviously, we can develop a
tool for modifying and editing this configuration file.
A web.config can appear in any directory on an ASP.NET Web application server. Said this,
if you have a web.config file in the directory "c:\inetpub\wwwroot", then the settings
specified in the web.config is applicable to all the subdirectories under wwwroot.
Each sub-directory can have its own web.config file and it will overwrite the settings
of the web.config file in the parent directory.
There is another file called machine.config, which provides configuration settings for
the entire server. If you change the contents of any web.config file then the change
will be immediately reflected in the processing of any incoming requests to the web'
server. These settings are calculated only once and then cached across subsequent requests.
ASP.NET automatically watches for file changes and will invalidate the cache if any of the
configuration files change. (For more information on caching
Click here)
The root element of a web.config file is always a <configuration> tag.
The <configuration> tag contains three different types of elements:
1) configuration section handler declarations,
2) configuration section groups, and
3) configuration section settings.
Following are the list of commonly used Configuation tags, that,
we be used in our web applications and will go thru them
1) Appsettings
2) Authentication
3) Authorization
4) Compilation
5) CustomErrors
6) Globalization
7) Identity
8) MachineKey
9) Pages
10) ProcessModel
11) SessionState
12) Trace
<appSettings>
This can be declared at the machine, site, application and subdirectory level
Include all the custom settings for your application in this section. Appsettings
tag contains two attributes viz; key and value.
<add key="key" value="value"/>
Eg: <add key="DSN" value="Server=moon;database=Store;Trusted_Connection=yes" />
<authentication>
All the authentication/security related stuff are declared in this section.
Authentication section contains a single attribute called "mode". Possible values for
"mode" are
(a) Forms (b) None (c) Passport and (d) Windows
Form based authentication can be used, if you want to use ASP .NET forms-based
authentication.
If you want to allow anyonmyous users to access your website, select none.
Passpost authentication can be used, if you want the authentication to be based on
Microsoft Passport authentication mode.
Use windows mode authentication, if you want to use Basic, Digest, Integrated
Windows authentication (NTLM/Kerberos), or certificates
Note: If you are using Form based authentication, then you have several other
options such as how the password should be encrypted, while submitting the form,
if login fails, which page should be shown to the user etc.
As the AuthenTication is included in, System.Web.Configuration.AuthenticationConfigHandler
while setting the authentication mode, you should code as follows
Eg:
<configuration>
<system.web>
<authentication mode="None" />
</system.web>
</configuration>
|
<authorization>
This is a very powerful tag, were you can restrict or allow users who wish to visit your
web site. Authorization tag contains two sub tags such as allow and deny.
Allow tag provides us with three attributes, namely users, roles and verbs. We can add
the list of users seperated by comma in the users attribute. Also we can specify the
role in which each user belongs too. Important aspect of the attribute verb is that,
we can control users depending upon the web request that the server is getting.
The verb attribute provides us with four options GET, HEAD, POST and DEBUG.
Deny tag has the same attributes as the allow tag has. Other aspect of both these tags
are, we can use two special symbols ? and * to specify anonymous users and "all users"
respectively.
Eg:
<configuration>
<system.web>
<authorization>
<allow roles="Admins" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
|
<compilation>
It is in this tag, you set all your compilcation options. This tag contains three sub-tags
and seven attributes, which are discussed below.
Attributes
debug specifies whether to compile retail binaries or debug binaries. True specifies debug
binaries and False specifies Retail binaries
defaultLanguage can be used to specify the language names to use in dynamic compilation files.
use explicit attribute to turn on explicit option or to turn off.
This takes either true or false, were true means explicit is enabled.
We can also do a batch compiliation by specifying the attribute bath as true. If we have
batch compiliation, then we might face the timeout problem. Then we may also want to use
the batchTimeout attribute to set the time for batch timeout.
numRecompilesBeforeApprestart is the next attribute. This attribute indicates the number
of dynamic recompiles of resources that can occur before the application restarts. This
attribute is supported at the global and application level but not at the directory level.
Strict attribute indicates the settings of the visual basic strict compile option. supports
two values, TRUE and FALSE.
SubTags
Compilers tag contains many or one compiler tag, were we define new compiler options.
Assemblies and Namespaces specifies ASP .NET processing directives
Eg:
<configuration>
<system.web>
<compilation defaultLanguage="VB" debug="true">
<compilers>
<compiler language="VB;VBScript" extension=".cls" type="Microsoft.VB. VBCodeProvider,System" />
<compiler language="C#;Csharp" extension=".cs" type="Microsoft.CSharp. CSharpCodeProvider,System" />
</compilers>
<assemblies>
<add assembly="ADODB" />
<add assembly="*" />
</assemblies>
<namespaces>
<add namespace="System.Web" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces>
</compilation>
</system.web>
</configuration>
|
<customErrors>
As the name says all about, customErros provides information about custom error messages
for an ASP.NET application. CustomErrors tag provides us with three attributes.
defaultRedirect can be used to specify the URL to direct a browser, if any unexpected
error occurs. The mode attribute takes three values On, Off or RemoteOnly. Remeteonly
specifies that custom errors are shown only to remote clients.
The subtag <error> might be very useful in a variety of way. We can specify the
error status code and ask the browser to redirect to a specific page. We should use
the attribute, statusCode to specify the error status code and the redirect attribute
to specify the redirect URL.
Eg:
<configuration>
<system.web>
<customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
|
<globalization>
Configures the globalization settings of an application. Two important attributes of this
tag are requestEncoding and responseEncoding. Default values for both encoding are
"iso-8859-1", which is English.
Eg:
<configuration>
<system.web>
<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1">
<globalization/>
</system.web>
</configuration>
|
<identity>
Controls the application identity of the Web application. Supports three attributes.
Impersonate is the first attribute, which specifies whether client impersonation is used
on each request to the web server. Takes either TRUE or FALSE. If the impersonation is FALSE,
then we should specify the values for the attributes, username and password.
Eg:
<configuration>
<system.web>
<identity impersonate="true" />
</system.web>
</configuration>
|
<machineKey>
Configures keys to use for encryption and decryption of Forms authentication cookie data.
This section can be declared at the machine, site, and application levels but not at the
subdirectory level. This tag supports three attributes; validationKey, decryptionKey and
validation.
ValidationKey and DecryptionKey takes the default value, which is AutoGenerate. We can also
specify a key and it should be length of 128 hexadecimal characters. The validation attribute
can be used to specify the alogrithm to be used while encryption. Possible values are
SHA1, MD5 and 3DES.
|