Pages

Advertisement

Thursday, August 30, 2007

Edit and Encrypt Web.Config Sections Using C# 2.0

ASP.NET 1.x allowed configurations in web.config file to be read from a .NET application. But, there were no options to manipulate Web.Config contents programatically. To achieve this, you had to consider the Web.Config file as a normal file or an XML file. .NET 2.0 fills this gap and also provides many other useful operations to be carried out on a Web.Config file, such as editing and encrypting sections of the Web.Config file. This articles illustrates these functionalities via a sample ASP.NET application.

The classes and methods to take control of the Web.Config file span across two namespaces:

Each section in the Web.Config file has a corresponding class in either of the namespaces. These classes allow modification of the corresponding sections. The classes for sections within the "system.web" section are found in System.Web.Configuration. Classes for other sections that are not specific to Web.Config are found in System.Configuration.

Modifying a section in Web.Config
  1. Open Web.Config for editing by using the WebConfigurationManager class.
  2. Use the respective Configuration class to make the necessary changes.
  3. Save the changes to the physical file by using the Configuration class.
private void UpdateConfig(string strKey, string strValue)
{
Configuration objConfig =
WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection objAppsettings =
(AppSettingsSection)objConfig.GetSection("appSettings");
if (objAppsettings != null)
{
objAppsettings.Settings[strKey].Value = strValue;
objConfig.Save();
}
}

In the above piece of code, the OpenWebConfiguration() method of the WebConfigurationManager class opens the Web.Config file in the root directory and returns it as a Configuration object. The GetSection() method of the Configuration class accepts the path to a specific section as an argument. The path is the relative path from the root node "configuration". You can refer to deeper nodes (sections, in this context) by their names separated by '/'. For example, to get access to the "authentication" section, provide "system.web/authentication" as the parameter to the GetSection() method. It returns a generic ConfigurationSecton object that can be typecast to the proper configuration section class. In this example, you get hold of the "appSettings" section with the help of the AppSettingsSection class. The AppSettingsSection class instance has a Settings collection property that contains the application setting from the configuration section as key-value pairs. The Settings property can be indexed using key to get the corresponding value. You also can set the value property and call the Save() method of the Configuration object to write configurations in the Configuration instance to the config file.

Deleting an entry in the Web.config file

The Remove() method of the Settings collection deletes an entry from the Configuration instance. The Remove() method accepts the key of the entry to be deleted.

Note: Please do not forget to call the Save() method of the Configuration instance to get the chnages reflected in the physical file.
objAppsettings.Settings.Remove("Location");

To iterate through all the key-value pairs in a configuration section, access the string array of keys via the AllKeys property of the Settings collection.

foreach (string strKey in objAppsettings.Settings.AllKeys)
{
DataRow dr = dt.NewRow();
dr["Key"] = strKey;
dr["Value"] = objConfig.AppSettings.Settings[strKey].Value;
dt.Rows.Add(dr);
}

Encrypting sections in Web.Config file

Now come the security issues. At times, the necessity for protecting sections the of config file arises. In .NET 2.0, there are options available to encrypt sections the of Web.config file programatically. The following method encrypts the "appSettings" section in the Web.config file.

private void EncryptAppSettings()
{
Configuration objConfig = WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection objAppsettings =
(AppSettingsSection)objConfig.GetSection("appSettings");
if (!objAppsettings.SectionInformation.IsProtected)
{
objAppsettings.SectionInformation.ProtectSection(
"RsaProtectedConfigurationProvider");
objAppsettings.SectionInformation.ForceSave = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}
}

The code above opens the Web.Config file for modification. It then retrieves the "appSettings" section. The ProtectSection() method of the SectionInformation class marks the configuration section for protection. It accepts the name of the protection provider to be used for the encryption. The ForceSave property indicates whether the specified configuration section will be saved even if it has not been modified. Finally, the Save() of the Configuration object writes the configuration settings to the Web.config file. The argument to the Save() method indicates the only properties modified needed to be written to the physical file.

Below is a listing of the "appSettings" section before encryption:


The encrypted "appSettings" section is listed below:


(
Full Size Image)

Decrypting sections of web.config file through code is practically identical. The UnprotectSection() method of the SectionInformation class removes the encryption from the configuration section.

private void DecryptAppSettings()
{
Configuration objConfig = WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
AppSettingsSection objAppsettings =
(AppSettingsSection)objConfig.GetSection("appSettings");
if (objAppsettings.SectionInformation.IsProtected)
{
objAppsettings.SectionInformation.UnprotectSection();
objAppsettings.SectionInformation.ForceSave = true;
objConfig.Save(ConfigurationSaveMode.Modified);
}
}

This encrytion and decryption functionality can be applied to other sections of web.config file also. It comes into use mostly for the "connectionStrings" section where the user name and password usually would be specified. This can done by creating a ConfigurationSection object. An example for the "connectionStrings" section is listed below.

ConfigurationSection objConfigSection = objConfig.ConnectionStrings;

The ConfigurationSection class represents a section within the configuration file. The Configuration class has propertes for each configuration section. This property can be used to get the respective ConfigurationSection objects. This is an alternative to the usage of the GetSection() method of the Configuration class.

Microsoft Releases Betas and More

If you've been paying attention, then you knew it was coming. Today Microsoft released Beta 2 of Visual Studio 2008 (previously codenamed "Orcas"). The new version of Visual Studio includes a number of new features including the over-hyped LINQ as well as built-in support for ASP.NET AJAX.

Also released with Visual Studio 2008 Beta 2 is a Community Technical Preview (CTP) for Silverlight Add-in for Visual Studio 2008. This add-in will allow you to start working with Silverlight from within Visual Studio 2008. it will also let you work with designer items from the Expression Blend 2 August Preview product.

Along with Visual Studio 2008 is the release of the .NET Framework 3.5 Beta 2. This new version of the Framework will now include full project templates and tools fro many of the things that were released in the .NET Framework 3.0.

A little more 'baked' is the release of Silverlight 1.0's release candidate (RC). This provides designers and tools for building rich internet applications. You can see a brief example of using Silverlight in our video, Using Microsoft Silverlight by Jacob Sanford.

Often you need to wait to use betas because you aren't allowed to release anything you develop to the public. Microsoft has announced a Go Live License so that you can deploy solutions created with Visual Studio 2008 Beta 2, the .NET Framework 3.5, and Silverlight 1.0 RC.

Downloads

If the downloads are not immediately available, they should be available by week's end (according to Microsoft):

Server-Based AJAX for Enterprise Applications

AJAX has been breaking new ground almost every day. With over 150 AJAX frameworks, there are plenty of options to choose from and, one might say, there are too many. Most of the AJAX frameworks provide you with browser extension libraries that will help you utilize the browser and interact with the server in a more productive way. What they are trying to do in most cases is to provide larger building blocks to achieve your goals by abstracting the calls to the browser API and to the server.

The last is a significant improvement over the plain vanilla browser coding, but you are still writing your application in a very poor environment with very poor developer tools. Your end result is a JavaScript application vulnerable in terms of security, manageability, and your IP protection.

When you are writing enterprise applications that are actually business apps, the security, productivity, manageability, and IP issues of AJAX development are becoming a real turn off. Applying SSL to your application can only bring you so far because you are still exposing server services for the client to consume through plain old JavaScript and running business logic that can easily be hijacked by using a simple script debugger. Also, the different AJAX frameworks still, in most cases, force you to write your application mainly in client-side JavaScript code that has its limits and productivity issues. The most obvious issue regarding JavaScript UI programming is the lack of design time capabilities, but that is only the tip of the iceberg.

Server-based AJAX is a recent addition to the AJAX frameworks arena; it basically changes the way you think about AJAX applications. It introduces a concept of server-based computing for web applications where your code runs entirely on the server and reflects changes to the client. This concept is used widely in remote desktop or application streaming software, but has not been available until now for web developers.

The server-based AJAX concept provides great benefits for developers in terms of productivity, manageability, and usability. Currently, the only available framework implementing full server-based AJAX is Visual WebGui, which provides full WinForms, such as API and design time capabilities for developing web applications. Although it sounds very much like Google's GWT, it is not because it does not serialize the application code to JavaScript but rather uses it at runtime on the server and reflects the UI to the client. This means that you have what can be called an "empty client" that is managed entirely by the server. Client events are sent to the server that, in turn, return its update commands to the client, reflecting changes made to the UI on the server. This process can be optimized in various scenarios explicitly to reduce the amount of server callbacks and bandwidth consumption, making the end result as responsive as standard AJAX applications.

There are some downsides to the server-based AJAX concept, mainly in terms of scalability, because you are utilizing a server session to run your application, but when applied to enterprise applications, rather then Amazon-sized sites, you get a very nice ROI that you would not get in any other architecture. In terms of responsiveness and performance, one would think that this concept lacks, but the opposite is the case; the server uses fewer resources in terms of CPU and IO because it does not have to receive/return large blobs and, most importantly, does not need to construct and deconstruct the application classes on every request. Also, as said before, there are many optimizations that can be applied to reduce server callbacks and bandwidth consumption.

The server-based AJAX concepts may just be the solution for enterprise AJAX applications because it provides these applications with the best of both worlds. Having a responsive, rich AJAX UI that behaves pretty much like desktop UI, but still maintaining the productivity, IP protection, and security of running the application on server side provides a compelling offering for enterprises.

Friday, August 24, 2007

Aero Glass: Create Special Effects With The Desktop Window Manager

I

Create your own effects with the sample code provided by microsoft for Aero Style building ... Here are some of the snaps how it appears ... now develop ur own vistle styles using the source codes ...

Figure 1 DWM Enables Features Like Flip 3D Task Switching
Figure 1 DWM Enables Features Like Flip 3D Task Switching

Figure 2 Slow Rendering Causes Tearing


Fiure 2 Slow Rendering Causes Tearing But it can be solved by this code

Figure 3 Glass Effects in Windows Media Player
Figure 3 Glass Effects in Windows Media Player

 

Figure 6 Window Color and Appearance Options
Figure 6 Window Color and Appearance Options

Figure 7 Windows Aero Glass Effects
Figure 7 Windows Aero Glass Effects (Click the image for a smaller view)

Figure 7 Windows Aero Glass Effects
Figure 7 Windows Aero Glass Effects (Click the image for a larger view)

Figure 12a Create a Live Thumbnail
Figure 12a Create a Live Thumbnail

Download Image NEW: Explore the sample code online! - or - Code download available at: DWM2007_04.exe (166KB)

READMORE

Saturday, August 18, 2007

Check Out The ASP.NET 2.0 Whidbey Features!

 

When ASP.NET 1.0 was released, it revolutionized Web application development by providing a rich set of features that were aimed at increasing developers' productivity. Now with ASP.NET 2.0 (code-named Whidbey), Microsoft increased the bar to a much higher level by providing excellent features out-of-the-box that are targeted towards reducing the code required for building Web applications.

These new enhancements arm the developers with a powerful platform that can make a significant impact in the way Web applications are developed and maintained. Apart from increasing the productivity of the developers, ASP.NET 2.0 also provides a number of excellent features, such as easy administration and management of the platform, an extensible platform that can be easily customized to meet the requirements of any enterprise, increased performance, and so on.

In this article, I will present you with the features of this new improved platform and to help you understand how you can use it to design, develop, and deploy enterprise-class Web applications. We will start off our discussion by taking a look at the changes to the ASP.NET Page architecture.

Changes to the Page Architecture

One of the significant improvements in ASP.NET 2.0 is the support for Single-Page architecture. This means that any Web page that you create can contain the server-side logic in the same page itself, instead of storing it in a separate .aspx.vb or .aspx.cs file, as is the case with current .NET version. The server-side code will be contained in a script block with a runat=server attribute. However, the object model and event model are still the same as in the current code-behind model.

Another important feature is the ability to request a Web form (.aspx file) from a browser without having to compile the code even once. When the page is first requested, ASP.NET compiles the page on the fly, dynamically generating the assembly. This makes it possible for you to resort to the "Just Hit Save" programming model (as similar to ASP) wherein you just develop the page and test the page without having to compile it.

Precompilation

As mentioned before, by default, ASP.NET Web pages and code files are compiled dynamically when a first request is made to the page. After the initial compilation, the compiled page is cached; the cache is used to satisfy the subsequent requests for the same page. Even though this approach is flexible, you should note that that when the page is requested for the first time, it requires a bit of extra time to compile the code. You can avoid this overhead by leveraging a new feature known as Precompilation; by using this feature, you can compile an ASP.NET Web site before making the Web site available to the users.

By using Precompilation, you also can catch all the compilation errors before deploying the application onto the production servers. ASP.NET 2.0 provides the following two options for precompiling a site.

  • In-place Precompilation—When you perform in-place precompilation, all ASP.NET files are compiled and stored in the special folder named codegen. The precompilation process follows the same logic that ASP.NET uses for dynamic compilation, also taking into consideration the dependencies between files. During precompilation, the compiler creates assemblies for all executable output and places them in the codegen folder. After the compiled output is created, ASP.NET fulfills requests for pages using the assemblies contained in this folder. One of the important advantages of precompilation is the ability to check the Web site for compilation errors. To precompile a Web application named NorthwindWeb (that is present in the default Web site), you need to enter the following URL in the browser.
    http://localhost/NorthwindWeb/Precompile.axd
    This command allows you to precompile the Web site and if there are any compilation errors, they will be displayed in the browser.
  • Precompiling a site for deployment—By using this option, you can create a special output that can be deployed to production servers. Once the output is created, you can deploy the output using various mechanisms such as XCOPY or FTP, or Windows installers onto the production servers. To precompile a Web site for deployment, you need to use the aspnet_compiler utility.

Use of Master Pages

ASP.NET 2.0 introduces a new concept known as Master Pages, in which a common base master file is created to provide a consistent layout for all the pages in your application. Once you isolate the look and feel and standard behavior for all the pages in your application, you can move them to a master page. In the master page, you also add placeholders (known as ContentPlaceHolders) for the content (or child pages) to add their custom content. When users request the content pages, the output of the content pages is merged with the output of the master page, resulting in an output that combines the layout of the master page with the output of the content page.

Master pages are saved with the file extension .master. Apart from containing all the contents that are required for defining the standard look and feel of the application, the master pages also contain all the top-level HTML elements for a page, such as <html>, <head>, and <form>. The master pages also contain one or more content placeholders that are used to define regions that will be rendered through the content pages. Let us take a look at an example.

For the purposes of this example, let us create a new page named MasterPageExample.master and add the following lines of code to it.

<%@ master language="C#" %>
<html>
<head runat="server">
<title>Master Page</title>
</head>
<body>
<form runat="server">
This content is from the Master Page from
which the all the pages will be inherited
<br />
<b>
<asp:contentplaceholder id="MiddleContent" runat="server">
</asp:contentplaceholder>
</b>
</form>
</body>
</html>

In the above lines of code, we start by defining a new page directive named master. As the name suggests, this declarative is used to identify that the current page is a master page and prevents the users from requesting the page from a browser. Then, inside the code, we define an element named asp:contentplaceholder, which will be used by all the content pages to render appropriate content that is specific to their pages. Now that we have had a look at the master page, let us look at the content page. To create a content page, let us create a new ASP.NET page named ContentPageExample.aspx and add the following lines of code to it.

<%@ page language="c#" master="~/MasterPageExample.master" %>
<script runat="server">
</script>

<asp:content id="Content1" contentplaceholderid="MiddleContent"
runat="server">
This content is from the child page
</asp:content>

The code required for the content page is very simple and straightforward. We start by defining the Page directive. As part of the Page directive, we also specify a new attribute named master that is used to identify the name of master page that we want to use. Since we have already created a master page named MasterPageExample.master, we will specify that as the master file. Next, we have an element named content that is used to associate the contentplaceholder element in the master page with the content page. This is done through the use of the contentplaceholderid attribute. That's all that is there to creating a master page and using the master page from a content page. Now, if you request the content page from a browser, you will get the following output.


Advantages of Master Pages

Master pages provide a clean approach for encapsulating the common functionality in a centralized location, allowing other pages to inherit from a single master page, thereby reducing the maintenance of the application.


  • Master pages make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
  • They provide a fine-grained control over the layout of the content pages by allowing you to control how the placeholder controls are rendered.
  • Master pages also expose an object model that allows you to customize the master page from individual content pages.

Sharing Code in the Application

Before ASP.NET 2.0, if you were to reference a reusable component from your ASP.NET application, you had to compile the assembly and place it in the bin folder (or place it in the GAC) of the Web application. But now, with ASP.NET 2.0, creating a reusable component is very simple and straightforward. All you need to do is to create the component in a pre-defined subdirectory called Code. Any component placed in this directory will be automatically compiled at runtime into a single assembly. This assembly is automatically referenced and will be available to all the pages in the site. Note that you should only put components in the Code subdirectory.

Creating Web Portals Using Web Parts

There are many times where you would want to allow the users of your Web site to be able to customize the content by selecting, removing, and rearranging the contents of the Web page. Traditionally, implementing this capability required a lot of custom code or you had to depend on third-party products to build a Web site of this nature. To address this shortcoming, ASP.NET 2.0 ships with a Web Parts Framework that provides the infrastructure and the building blocks required for creating modular Web pages that can be easily customized by the users. You can also use Web Parts to create portal pages that aggregate different types of content such as static text, links, and content that can change at runtime. It is also possible for the users to change the layout of the Web parts by dragging and dropping from one place to another, providing a rich user experience.

New Data Controls

One of the limitations of ASP.NET 1.0 is that it did not provide a declarative model for binding data to data-aware controls such as DataGrid, DataList, and Repeater. Now, in ASP.NET 2.0, you have a very powerful and easy-to-use declarative model for binding data directly from the database. To this end, ASP.NET 2.0 provides a number of new data-bound controls. Before we take a look at this new set of controls, let us understand the theory behind this new declarative model. Binding data to data-aware controls requires the following two steps:


  1. You need to create a data source control that is responsible for reading and writing data from the database. Data source controls encapsulate common functionality such as reading, writing, deleting, and inserting from the database.
  2. Next, you need to bind the data-bound controls, such as a dropdown list or a checkbox, to the data source control.

ASP.NET 2.0 provides the following data source controls:


  • <asp:SqlDatasource>—This data source control is designed to work with SQL Server, OLE DB, ODBC, and Oracle databases. As the name suggests, this control enables you to select, update, delete, and insert data using SQL commands. In the later section, we will see an example of this control.
  • <asp:ObjectDatasource>—For reasons of clean separation and easier maintenance, most of the Web applications are constructed using N-Tier principles. When you work with an N-tier application, it is most likely that your middle layer objects may return complex objects that you have to process in your ASP.NET presentation. Keeping this in mind, Microsoft has created this new control that allows you to seamless integrate the data returned from the middle layer objects with the ASP.NET presentation layer.
  • <asp:AccessDatasource>—As you can see from the name, this control is designed to work with Access databases.
  • <asp:XmlDatasource>—This control allows you to bind to XML data, which can come from a variety of sources such as an external XML file, a DataSet object, and so on. Once the XML data is bound to the XmlDataSource control, this control then can act as a source of data for data-bound controls such as TreeView and Menu.
  • <asp:DataSetDatasource>—The DataSetDataSource control allows you to easily switch between an XML and a relational view of the data. By using this control, you also can specify an XSLT Transformation to restructure the XML data.
  • <asp:SitemapDatasource>—This control allows the users to navigate between the pages in the Web site. To perform this, you need to create an XML file named app.sitemap that lays out the pages of the site in a hierarchical fashion. Once you have the site hierarchy in the app.sitemap file, you then can data-bind the SitemapDataSource control with the app.sitemap file. After that, the contents of the SitemapDataSource control can be bound to data-aware controls such as TreeView and so on.

Apart from the above data source controls, ASP.NET 2.0 also provides the following data-bound controls that you will normally use to display data that is contained in the data source controls:


  • <asp:gridview>—This control is the successor to the DataGrid control that was part of ASP.NET 1.0. Like the DataGrid control, this control is used to display the values of a data source in a table. In a grid view control, each column represents a field, while each row represents a record. As you would expect, you can bind a grid view control to a SqlDataSource control, as well as any data source that implements the System.Collections.IEnumerable interface. This control also can adaptively render data for different types of devices and browsers that are making the request.
  • <asp:detailsview>—As the name suggests, this control can be used in conjunction with the grid view control and can be used to display the details of a specific record in the data source.

The following example demonstrates how to use the combination of SqlDataSource and GridView controls to retrieve and display data from the Categories table in the Northwind database without writing even a single line of code.

<%@ page language="C#" %>
<script runat="server">
</script>

<html>
<head id="Head1" runat="server">
<title>Data Binding using SqlDataSource control</title>
</head>
<body>
<form id="Form1" runat="server">
Enter the Category ID: <asp:textbox id="CategoryID"
runat="server"></asp:textbox>
<asp:button runat="server" id="btnGet" text="Get Category"/>

<asp:sqldatasource id="categoriesSource" runat="server"
connectionstring="server=localhost;database=northwind;
uid=sa;pwd="
selectcommand="SELECT * From Categories
Where CategoryID=@CategoryID">
<SelectParameters>
<asp:ControlParameter defaultvalue="1" Name="CategoryID"
ControlId="CategoryID" PropertyName="Text"/>
</SelectParameters>
</asp:sqldatasource>
<asp:gridview datasourceid="categoriesSource"
runat="server" id="gridCategories">
</asp:gridview>
</form>
</body>
</html>

In the above code, we start by declaring a button control named btnGet. This control is just used to perform a post-back to the server so that the SQL query can be re-evaluated. Then, we declare a SqlDataSource control named categoriesSource. While declaring the SqlDataSource control, we also specify the ConnectionString and the SQL statement to be executed as attributes. As you can see from the SelectCommand attribute value, we specify a placeholder for the CategoryID parameter by using the @CategoryID identifier. Then, we specify the value for the SQL query parameter by using the SelectParameters template. The combination of ControlId and PropertyName is used to specify the name of the control and the property of the control (that will return the value of the textbox in this case). This allows the category ID entered in the CategoryID textbox to be used as an argument to the SQL query. For the first time, we specify the default value of 1 for the CategoryID by using the defaultvalue property.

When you execute the above code, you will see an output that is somewhat similar to the following.


By default, when the above page comes up, it displays the details of the category that is identified by CategoryID 1. After that, if you enter a category ID in the textbox and click on Get Category, it will display the details of the category based on the value entered.

Advanced Features of Data Controls

Some of the advanced features provided by the data controls are as follows:


  • By handling all the low level tasks such as opening connection to the database, executing a command, retrieving the results of the command, and closing the connection, data source controls allow you to focus on the core business logic of the application.
  • Apart from providing a powerful declarative model for creating rich data-driven Web applications, ASP.NET 2.0 also allows you to access the same set of features programmatically, retaining the flexibility of the database features.
  • By using the declarative model provided by data controls, not only you can execute stored procedures but also pass parameters to the stored procedures. For example, you can retrieve the value of a Textbox and pass it as a parameter to a stored procedure that can return appropriate rows depending on the value entered in the textbox.
  • Another excellent feature of data source controls is that they all offer the same basic Object model and API for you to program against, reducing the learning curve required for understanding the different types of controls.

Caching Enhancements

The Cache API introduced with ASP.NET 1.0 was a powerful feature that can be immensely useful in increasing the performance of a Web application. The Cache API also allows you to invalidate items in the cache based on some pre-defined conditions such as change in an XML file, change in another cache item, and so on. By using this feature, you can remove or invalidate an item from the cache when the data or another cached item changes. However, the Cache API in ASP.NET 1.x versions did not provide a mechanism to invalidate an item in the cache when data in a SQL Server database changes. This is a very common capability that many Web applications require.

Now, with ASP.NET 2.0, Microsoft has introduced a new cache invalidation mechanism that works with SQL Server as well. By using this new capability, you can invalidate an item in the Cache object whenever the data in a SQL Server database changes. This built-in cache invalidation mechanism works with SQL Server 7.0 and above. However, with SQL Server 7.0 and 2000, only Table-level cache invalidation mechanism is supported.

The next release of SQL Server (code-named Yukon) also will feature a row-level cache invalidation mechanism providing a finer level of accuracy over the cached data. To enable the SQL Server-based cache invalidation mechanism, you need to do the following:


  • Add the OutputCache directive to the top of the page. The OutputCache directive also contains a new attribute named sqldependency and the value supplied to this attribute should be of the form <DatabaseName>:<TableName>. In this example, we are using the Employees table in the Northwind database.
    <%@ OutputCache duration="3600" varybyparam="none"
    sqldependency="Northwind:Employees" %>

  • The next step is to add the appropriate settings to the web.config file. After modification, the web.config file should look like the following:
    <?xml version="1.0"?>
    <configuration>
    <connectionStrings>
    <add name="Northwind"
    connectionString="server=localhost;database=Northwind;
    UID=sa;PWD="/>
    </connectionStrings>
    <system.web>
    <cache>
    <sqlCacheDependency enabled = "true"
    pollTime = "1000" >
    <databases>
    <add name="Northwind"
    connectionStringName="Northwind"
    pollTime = "1000"/>
    </databases>
    </sqlCacheDependency>
    </cache>
    </system.web>
    ---------
    ---------
    </configuration>

    By using the above configuration entries, we specify the name of the database in which we want to enable the cache notification mechanism.


  • After that, we need to enable the specific tables in the Northwind database for notification. You can perform this using any one of the following two ways:

    • Using the aspnet_regsqlcache
    • Using the EnableTableForNotifications method of the SqlCacheDependencyAdmin class. Once we configure the table to send notifications, any time data in the table changes, it notifies the ASP.NET to invalidate the specific item in the cache.

SP.NET Personalization

There are many times when you would want to store and present information that is unique to a user. When a user visits your site, you can use the information that you have already collected from the user to present the user with a personalized version of your Web application. Traditionally, implementing this personalization capability required you to go through the following steps:


  • You must store the information about the user using a unique user identifier. This information is required to recognize the user when they visit again.
  • You need to fetch the user information as needed.
  • Finally, present the user with the personalized content.

To simplify your applications, you can use ASP.NET personalization, which can abstract all of the above complexities from you. In ASP.NET personalization, information about a specific user is stored in a persistent format. ASP.NET personalization allows you to manage user information easily without requiring you to create and maintain your own database. In addition, the personalization system makes the user information available using a consistent, easy-to-use, strongly typed API that you can access from anywhere in your application. You also can store objects of any type in the personalization system, including user information, user preferences, or business information. The personalization system uses a generic storage system for storing the data and makes that data available to the users in a type-safe manner. By default, ASP.NET 2.0 uses SQL Server as the storage mechanism.

Security Enhancements

One of the important security enhancements made in ASP.NET 2.0 is the new role management system that helps you to manage authorization, allowing you to specify the resources that users in your application are allowed to access. ASP.NET Role management system lets you treat groups of users as a unit by assigning users to roles such as manager, sales, member, and so on. In Windows, you create roles by assigning users to groups such as Administrators, Power Users, and so on. After you have established roles, you can create access rules in your application. For example, your site might include a set of pages that you want to display only to members. Similarly, you might want to show or hide a part of a page based on whether the current user is a manager. With roles, you can establish these types of rules independently of individual application users.

In this new role management system, the provider of the roles is completely separated from the role management API. ASP.NET supports the following providers to maintain role information:


  • Access—In this case, role information is stored in a Microsoft Access database and this is the default provider.
  • SQL Server—Here, the role information is stored in a SQL Server database.
  • Windows—In this case, the role information is based on Windows accounts.

ASP.NET provides a number of new controls that are built on top of the ASP.NET role management system. Some of the new controls are as follows:


  • <asp:login>—Provides a standard login capability that allows the users to enter their credentials
  • <asp:loginname>—Displays the name of the logged-in user
  • <asp:loginstatus>—Indicates whether the user is authenticated or not
  • <asp:loginviewv—Provides various login views depending on the selected template
  • <asp:passwordrecovery>—Capability for e-mailing the users their lost password

ASP.NET Themes

Themes are rich skin templates that allow you to define the look of pages and controls, which then can be applied to all the pages in your application, providing a consistent look and feel for the entire application. Themes are extremely flexible in that they can be applied to an entire Web application, to a page, or to an individual control.

Theme files are stored with the extension .skin and all the theme files for a Web application are stored in the special folder named Themes. ASP.NET ships with several themes out of the box. However, it also is possible for you to create custom theme files. To create a custom theme and apply it to a specific page, you need to go through the following steps:


  • Create a file with the extension .skin and add all the controls (that you want to use in a page) and their style properties. Remember to remove the ID attribute in all of the controls. For example, you can use the following code to define the theme for a button control.
    <asp:Button runat="server" BackColor="Black"
    ForeColor="White" Font-Name="Arial"
    Font-Size="10px" />

  • Once you have created the .skin file, you then can apply that theme to all the pages in your application. To apply the theme to a specific page, all you need to do is to add the Theme attribute to the Page directive, as shown below.
    <%@Page theme="Testheme" %>

    It also is possible for you to programmatically access the theme associated with a specific page by using the Page.Theme property. Even though themes are very similar to CSS Style Sheets, they differ from style sheets in the following ways:


    • Themes can define many properties of a control or page, not just a specific set of style properties.
    • Themes can also include external files such as graphics and so on.

Other Features

In this section, we will browse through the remaining important features of ASP.NET 2.0.


  • Within Whidbey, Intellisense (Statement Completion) is now available in the server side <script> blocks. It also is available in <% %> blocks, and in Page directives (starting with the letter @).
  • ASP.NET 2.0 is 64-bit enabled.
  • ASP.NET 2.0 will be almost completely backward compatible with ASP.NET 10.0 and ASP.NET 1.1.
  • ASP.NET 2.0 comes bundled with a built-in development Web server, obviating the need to install IIS.
  • You also can define a single class in multiple files; at runtime they will be compiled together to create a single assembly.
  • With ASP.NET 2.0, you can perform postback across pages, meaning that you can postback from one page to another page. To perform cross-postback when the user clicks a button, set the PostTargetUrl property on the button control to the URL of the new page. From within the new page, you can reference the original page by using the PreviousPage property.
  • In this release, all the <asp:> built-in controls will provide native support for mobile devices, obviating the need to learn a new <mobile:> programming language to program for mobile devices.
  • In the health monitoring space, it will provide support for automated notification when exceptions occur in the Web site. For example, ASP.NET can automatically send an e-mail to the admin when an exception occurs in the Web site.

Conclusion

As you can see, ASP.NET 2.0 provides a number of new productivity enhancements for the developers to be excited about. The features we looked at in this article represent just the tip of the iceberg. But, they should help you get a kick start on this new and feature-rich platform. Once you get familiar with this new platform, you will have a rich set of tools and techniques that can go a long way in making the development of a Web application a breezy experience.


LINKBACK