ASP.NET Web Parts controls are an integrated set of controls for creating Web sites that enable end users to modify the content, appearance, and behavior of Web pages directly in a browser. The Web Parts control set consists of three main building blocks:
- Personalization,
- User interface (UI) structural components, and
- Actual Web Parts UI controls.
- Personalization: Web Parts use a Personalization Provider. This is so users that visit the site get a different perspective, depending on their personalization. One user might want a red background while another might want blue. This is their personalization, and it is stored in a database. By default, Web Parts are configured to use Sql server 2005 Express edition. For another version of Sql server 2005, there are few things which we must do to make it work smoothly.
First of all, we need to create a personalization database named aspnetdb. By default, Web Parts look for this database to keep the user personalization information. In order to do this, we must use the 'aspnet_regsql.exe' wizard. Browse out to '%windir%\Microsoft.NET\Framework\' and run this command. Once the wizard is up, choose to 'Configure SQL Server for application services'. Select the correct server name (if using a named instance, be sure to include the server and named instance, such as 'fileserver\sql2k5instance'). If using SQL Server authentication, put in your username and password. Select the database that will serve as your personalization database. Now database is set up for membership and personalization.
Now add a connection string to Web.config file with the key name ‘localSqlServer’.
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Server=G01BR1P264; Database=aspnetdb; Integrated Security=SSPI; MultipleActiveResultSets=true;"providerName="System.Data.SqlClient"/>
connectionStrings>
The name of the connection string is important, as Web Parts look by default for a connection string named 'LocalSqlServer'. The data source is equally important and should reflect our database instance (be sure to include the name if using a named instance). Since I have set up the 'aspnetdb' database as my personalization database, this will be what I use as the Initial Catalog. Also, since only SQL Server 2005 Express Edition supports User Instances, I will set this property to 'false'.
Apart from these, either you have to user Window Authentication or Form Authentication.
Window Authentication: To use Window Authentication, Add the following to the Web.config.
<authentication mode="Windows">authentication>
Also, open the Inet Manger of your server, point to your website and open properties. Under the Directory security tab click on ‘Edit’ Anonymous access and Authenication control.
Uncheck the Anonymous Access and check mark the integrated Windows authentication.
Form Authentication: To use Form Authentication, Add the following in the web.config
<authentication mode="Forms">
<forms name=".ASPXAUTH" loginUrl="Login.aspx" defaultUrl="Home.aspx" cookieless="UseUri" />
authentication>
Here loginUrl will be your login page name and defaultUrl will be your Home Page. Cookieless is the option property, if false, a cookie will be saved to a user machine.
Now we have to user the FormAuthentication class and it’s static member to add the user as authenticated.
For example: suppose we have another database where we keep the user list and their login credentials.
Add the connection string information of this database also to web.config. During login process, check the user input against the duser credentials store in a database table. If it is true then redirect user to home page using
FormsAuthentication.RedirectFromLoginPage(UserName, false);
This is a static method of the FormAuthentication Class which add the user to authenticated list and redirect to default page mentioned in a web.config.
Now we can whether user has been authenticated or not by
HttpContext.Current.User.Identity.IsAuthenticated Which return true in case of authenticated user.
There is one another method which is used to make user log out.
FormsAuthentication.SignOut();
- User interface (UI) structural components: