Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Profiles in practice

Good sites are capable of remembering their users. They know their age, address, their preferences, ultimately, even their favorite color for the background of the site.

Profiles in practice

 

Good sites are capable of remembering their users. They know their age, address, their preferences, ultimately, even their favorite color for the background of the site. Everything in order to please you and make it in such a way that you will return in the future. But after all, how much work is applied to embed this level of personalization in our sites?

 

Traditionally, a great amount of work would be necessary. You would need some tables in a database, some queries, Stored Procedures and to develop classes to store and recover this information. Not to speak of the work spent when the need to include new fields of personalization comes to be.

 

This so far. We will see in this article that with the ASP.NET 2.0 we can use Profiles to create personalized sites, with the minimum amount of work.

 

Web.config

Without losing any more time, let us see how the Profiles work in practice. Open the Visual Studio 2005 and create a new ASP.NET WebSite. In the examples of this article we are using the C# language. We are going to include in our project a Web.config file, where we will configure the Profile fields.

 

In the Solution Explorer, right-click the WebSite, choose the Add New Item option and click on Web Configuration File. In Name maintain “Web.Config” and click on Add. Modify the Web.config so that it is the same as that demonstrated in Listing 1. Note that in this example we remove all the comments of the file.

 

Listing 1. Web.config

<?xml version="1.0"?>

<configuration>

    <appSettings/>

    <connectionStrings/>

    <system.web>

        <authentication mode="Windows" />

        <profile>

          <properties>

            <add name="Name" />

            <add name="Gender" />

            <add name="Age" type="System.Int16"/>

          </properties>

        </profile>

        <compilation debug="false" />

    </system.web>

</configuration>

 

Observe that we have a section called Profile, and inside that we have Properties. In this section we can determine the information that we wish to store in the user profile. See that we are using the Windows authentication form, however the Profiles work independent of the user authentication form.

 

We are creating three properties in the Profile, the first two will be created as String, due the type has been suppressed. And the property Age was declared as System.Int16 type, also known as short.

 

The Profile object

To continue with our example, we will create an interface in the Default.aspx page, where we will create three fields (Figure 1).

 

rsanppfig01.jpg 

Figure 1. Default.aspx page layout

 

Each one of the fields corresponds to one of the properties of the Profile. Note that for the Name and the Age we use TextBoxes (with the respective IDs “TextName” and “TextAge”), and for the Gender we used the DropDownList (“ListGender”). Double click over the Save button and include the code in Listing 2 to your event Click.

 

Listing 2. Storing the user Profile

protected void ButtonSave_Click(

  object sender, EventArgs e)

{

    Profile.Name = TextName.Text;

    Profile.Gender = ListGender.SelectedValue;

    Profile.Age = short.Parse(TextAge.Text);

}

 

As we can verify in the former list, an ASP.NET page have an object called Profile, which contains the properties that we have defined in the Web.Config. Note that if you create new properties, exclude or modify the created properties, automatically these are updated in the Profile object.

 

Observe in Figure 2 the Age property being accessed through the IntelliSense, where in your ToolTip is shown the short type, exactly as we have defined in the Web.config.

 

rsanppfig02.jpg 

Figure 2. Accessing properties of the Profile object.

 

In Listing 2 we are storing the supplied values by the controls directly into the properties of the Profile object. Due the fact that we are in the code of the Default.aspx page, include the code from Listing 3 into the Page_Load.

 

Listing 3. Recovering data from the Profile in the page’s controls.

protected void Page_Load(object sender, EventArgs e)

{

   if (!IsPostBack)

   {

      TextName.Text = Profile.Name;

      ListGender.SelectedValue = Profile.Gender;

      TextAge.Text = Profile.Age.ToString();

    }

}

 

See that we are doing exactly the opposite of what wee did in the button. In case of the page not perform a Postback, we are recovering the data from the Profile and showing these in the page’s controls.

 

Testing the Profile

Shall we make a test? Save, compile and run the project. Such as is shown in Figure 3, fill the form controls, and next click the Save button.

 

rsanppfig03.jpg 

Figure 3. Storing data in the Profile.

 

And without delay, close the page, run the application again and see that the saved data are readily displayed in the controls. You will see that the data will be recovered, except if we are run the application with another user.

 

Where and how was the data stored?

Some points need to be mentioned here. First of all, where was the data stored? Alike other ASP.NET 2.0 features (Membership, Roles, WebParts), the properties of the Profiles are stored in a database called aspnetdb, that the ASP.NET itself creates in the Express version of the SQL Server.

 

Problems with the aspnetdb in the SQL Server 2005 Express?

In case you have problems because the aspnetdb database is not registered in the SQL Server 2005 Express Edition, you can use the aspnet_regsql.exe (C:\Windows\Microsoft.NET\Framework\v2.0.50727) to create the database.

Also check, in the machine.config file (C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG), if in the <ConnectionStrings> node the entry LocalSqlServer is set correctly towards the created database.

 

Go to the Server Explorer and create a new connection that point to the aspnetdb database (Figure 4), expand this connection and see that in Tables we have the aspnet_Profile table. This is the table where the ASP.NET stores the properties of the Profile of each user that accesses the application.

 

rsanppfig04.jpg 

Figure 4. aspnet_Profile table of the aspnetdb database.

 

Another important observation that we have to make is that at no moment we fired SQL commands in the database. The properties of the Profile object were automatically persisted by the ASP.NET through the SqlProfileProvider.

 

As shows Figure 5, the SqlProfileProvider is an ASP.NET class which inherits from the ProfileProvider abstract class, and is responsible for persisting the properties of the Profile in the aspnetdb database.

 

rsanppfig05.jpg 

Figure 5. ProfileProvider and SqlProfileProvider classes.

 

The Provider is a model of the ASP.NET used for persistence of data, and is widely customizable. In case you wish to persist the Profile properties in another database, which is not the SQL Server, or in XML files, for example, it is enough to create your own Provider class, inheriting the ProfileProvider class and implementing the necessary methods.

 

Default Values

Let us now explore some options of the properties of the Profile. A very useful attribute that can be configured in the property itself is the defaultValue. See in Listing 4, how to define the defaultValue in a property.

 

Listing 4. Defining property with default value.

<profile>

    <properties>

        <add name="Name" />

        <add name="Gender" />

        <add name="Age" type="System.Int16"/>

        <add name="City" defaultValue="Sao Paulo"/>

    </properties>

</profile>

 

Observe, that we are creating a new property called City, where we define the defaultValue as “Sao Paulo”. Run a test, such as we have done for the other properties, include that in the Web.config, create the field in the interface and the code necessary to recover and store the City property of the Profile. Run the project (Figure 6).

 

rsanppfig06.jpg 

Figure 6. Testing a default value of the City property.

 

Anonymous users

As default, the Profile functionality is not authorized for anonymous users, only for authenticated users. But it is possible to enable the Profile feature for the anonymous access to the site, just configure that in the Web.Config. See an  example in Listing 5.

 

Listing 5. Authorizing the Profile for anonymous users.

<?xml version="1.0"?>

<configuration>

  <system.web>   

    <anonymousIdentification enabled="true"/>

    <profile>

      <properties>

        <add name="Name" allowAnonymous="true" />

        <add name="Gender" allowAnonymous="true" />

      </properties>

    </profile>

    <compilation debug="false" />

  </system.web>

</configuration>

 

See in this example that we are firstly defining the anonymousIdentification to true, in such a way that the anonymous users will be able to identified by the application. And for the properties that we wish to enable for anonymous user, we need to include the allowAnonymous=true attribute.

 

Groups

Another very interesting functionality that we have in the properties of the Profile is the possibility to organize them in groups. Modify the properties of the Profile in your Web.config so that they become as that demonstrated in Listing 6.

 

Listing 6. Grouping Properties of the Profile.

<profile>

   <properties>

        <add name="Name" />

        <add name="Gender" />

        <add name="Age" type="System.Int16"/>

        <group name="ShippingAddress">

          <add name="Address"/>

          <add name="City" defaultValue="Sao Paulo"/>

          <add name="Country"/>

        </group>

   </properties>

</profile>

 

See that we have created a new element in the properties, called group. Within this group we are defining the properties that refer to the delivery address of the user. The upside of grouping the properties like this is, is that the way in which they will be displayed in the Profile object is far more organized (Figure 7).

 

rsanppfig07.jpg 

Figure 7. Properties of the Profile organized in Groups.

 

The Profile object is organized in groups, which makes things easier when we are working with a large amount of  properties.

 

The ProfileManager

A very important functionality for those who wish to manage the Profiles of the application  is the ProfileManager class (Figure 8), which is a static class (class where we can use the methods without instancing an object).

 

rsanppfig08.jpg 

Figure 8. ProfileManager class.

 

Through the class, we can access data from all the Profiles with the GetAllProfiles method, or yet we may search for a specific Profile through the user name, using the FindProfilesbyUserName method.

 

See that there are several methods for the management of the Profiles. Add a Label in Default.aspx and include the code in Listing 7 to the Page_Load (after the code that load the profile data to the controls).

 

Listing 7. Listing all the Profiles

using System.Web.Profile;

...

ProfileInfoCollection Profiles =

  ProfileManager.GetAllProfiles(

    ProfileAuthenticationOption.All);

foreach (ProfileInfo Profile in Profiles)

{

  Label1.Text += Profile.UserName + "<br>";

}

 

We are recovering all the Profiles of the application and storing in an object of the ProfileInfoCollection class, afterwards through a foreach, we are listing the user name of each Profile in a Label that was inserted in the page. Save, compile and run the project (Figure 9).

 

rsanppfig09.jpg 

Figure 9. Listing all the Profiles through the ProfileManager.

 

Conclusion

We were able to verify in this article that the ASP.NET 2.0 brings many functionalities for the creation and management of the application user’s Profiles. We saw only the main resources of the Profiles. In a more advanced level you can create your own customized Profile classes, being enough to inherit the ProfileBase class and include the properties it needs.

 

With the use of Serialization, it is also possible to create complex type properties, such as for example, a property which is an object of the CustomerData class. In this manner we could store not only a string which identifies the kind of client, but an object with all the properties and methods of the CustomerData class. 

 

Finally, the Profile is an excellent and extensible resource to develop our sites with customization functionalities.



colunista nao disponivel

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login