Monday, 30 July 2012

ASP.NET Profile Properties in web.config


In many applications, you want to store and use information that is unique to a user. When a user visits your site, you can use the information you have stored to present the user with a personalized version of your Web application. Personalizing an application requires a number of elements: you must store the information using a unique user identifier, be able to recognize users when they visit again, and then fetch the user information as needed. To simplify your applications, you can use the ASP.NET profile feature, which can perform all of these tasks for you.
The ASP.NET profile feature associates information with an individual user and stores the information in a persistent format. Profiles allow you to manage user information without requiring you to create and maintain your own database. In addition, the ASP.NET profile feature makes the user information available using a strongly typed API that you can access from anywhere in your application.
You can store objects of any type using profiles. The profile feature provides a generic storage feature that allows you to define and maintain almost any kind of data while still making the data available in a type-safe manner.

How ASP.NET Profiles Work
To use profiles, you first enable profiles by modifying the configuration file for your ASP.NET Web application. As part of the configuration, you specify a profile provider, which is the underlying class that performs the low-level tasks of storing and retrieving profile data. You can use the profile provider included with the .NET Framework, which stores profile data in SQL Server, or you can create and use your own profile provider. You can specify an instance of the SqlProfileprovider that connects to a database of your choosing, or you can use the default instance of the SqlProfileprovider that stores profile data on the local Web server.
You configure the profile feature by defining a list of properties whose values you want to maintain.
Web.config
<system.web>
    <profile>     
      <properties>
        <add name="Name" defaultValue="Ravi" allowAnonymous="true" />
        <add name="Postal" type="System.Int64" allowAnonymous="true" />
        <group name="UI">
          <add name="MasterPage" defaultValue="Layout1.master"/>
          <add name="Theme" defaultValue="Red" />
        </group>
        <add name="Pet" allowAnonymous="true" type="Pet" />       
      </properties>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>
</system.web>

....
....
<connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

Pet.cs
using System;

public class Pet
{
    public Pet()
    {
        // default ctor required for serializer
    }

    public Pet(string name)
    {
        _name = name;
    }

    private string _name = String.Empty;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}


ProfileTest.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            txtName.Text = Profile.Name;
            txtPostal.Text = Convert.ToString(Profile.Postal);
            txtMaster.Text = Profile.UI.MasterPage;
            txtTheme.Text = Profile.UI.Theme;

            Profile.Pet = new Pet("Cat");
            txtPet.Text = Profile.Pet.Name;
        }
}

protected void btnSetVal_Click(object sender, EventArgs e)
{
        Profile.Name = txtName.Text;
        Profile.Postal = Convert.ToInt64(txtPostal.Text);
        Profile.UI.MasterPage = txtMaster.Text;
        Profile.UI.Theme = txtTheme.Text;
        Profile.Pet.Name = txtPet.Text;
}

protected void btnGetVal_Click(object sender, EventArgs e)
{
        Response.Write("<br/> New Profile Values: <br />");
        Response.Write("<br/>" + Profile.Name);
        Response.Write("<br/>" + Profile.Postal);
        Response.Write("<br/>" + Profile.UI.MasterPage);
        Response.Write("<br/>" + Profile.UI.Theme);
        Response.Write("<br/>" + Profile.Pet.Name);
}

No comments:

Post a Comment