ASP .NET 2.0 Profile Object – Part II by azamsharp

Introduction:

In the last article you learned the basics of the Profile object which is introduced in ASP.NET 2.0

In this article we will learn some of the more cool features of the Profile objects which includes Groups, Complex Types and migrating anonymous to authenticated user.

Using Groups with Profile:

In the last article I showed you that how you can make properties in the web.config file and use those defined properties in your code behind. The properties that we defined in the last article were common to all and hence every user exposes the same properties. In a multi user and multi role environment you may need to expose some properties only for a group of users. Profile object let’s you distinguish users based on their groups.

Creating Groups:

Let’s see how we can create groups. The only thing you need to do is to add the settings in the configuration file. Take a look at the settings below which makes two groups Students and Faculty.

<anonymousIdentificationenabled="true"/><profiledefaultProvider="MyProfileProvider">
<providers>
<addname="MyProfileProvider"type="System.Web.Profile.SqlProfileProvider"connectionStringName="ConnectionString"/>
</providers>
<properties>
<groupname="Student">
<addname="ID"allowAnonymous="true"type="System.Int32" />
<addname="Name"allowAnonymous ="true"/>
<addname="Major"allowAnonymous ="true"/> 
</group>
<groupname="Faculty">
<addname="ID"allowAnonymous="true"type="System.Int32"/>
<addname="Name"allowAnonymous="true"/>
<addname="CourseList" allowAnonymous="true"  type="System.Collections.ArrayList" />
</group> 
</properties>
</profile>

The Student group exposes different properties than the Staff group. One more important thing to note is the property name CourseList which is of ArrayList type. You can use any datatype within the properties section whose base class allows serialization. I will talk about this later in this article in a little detail.

Now, let’s see how we can use the Groups that we have defined in the web.config file.

Profile.Student.Name = "Azamsharp";Profile.Faculty.Name = "Dr Smith";

See how easy it is and it is pretty much the same that we have already done in the first article. Once again we get the full intellisense support when using groups in the Profile object.

You can also easily populate the ArrayList of the Faculty member with come dummy courses. Take a look at the code below to see how we populate the ArrayList of a Faculty member.

Profile.Faculty.Name = "Dr Smith";Profile.Faculty.CourseList.Add("Math");
Profile.Faculty.CourseList.Add("English");
Profile.Faculty.CourseList.Add("Accounting");

And you can easily retrieve the values in the other page using the following piece of code:

string name = Profile.Faculty.Name;System.Collections.ArrayList courseList = Profile.Faculty.CourseList;

 

Also, remember that the next time you add in the Profile object it won’t be empty but it will have 3 records which you inserted previously.

Complex Types:

You can even use Profile object with the Complex Types which includes Entity Classes.

Let’s first make a very simple entity class.

Here is a very simple class:

[Serializable]public class User 
{

   /* PRIVATE FIELDS */ 
   private string _firstName;
   private string _lastName;
   /* PUBLIC PROPERTIES */ 
   public string FirstName
   {

      get 
      {
         if (!String.IsNullOrEmpty(_firstName))
              return _firstName;
        else return null;
       }

       set { _firstName = value; }
}

public string LastName
{

      get 
      {
           if (!String.IsNullOrEmpty(_lastName))
              return _lastName;
           else return null;
      }

       set { _lastName = value; }
}

   public User()
   {

    }

}

When using Profile object with the entity class you have to mark the class Serializable.

Now, let’s see the settings in the web.config file.

<anonymousIdentificationenabled="true"/><profiledefaultProvider="MyProfileProvider">
<providers>
<addname="MyProfileProvider"type="System.Web.Profile.SqlProfileProvider"connectionStringName="ConnectionString"/>
</providers>
<properties>
<addname="User"type="User"serializeAs="Binary"allowAnonymous="true"/> 
</properties>
</profile>

The name attribute simply represents the name of the Profile. The serializaAs attribute represents that what will be the format of the serialized object. In this case I have defined the format as Binary but you can also choose XML, String or ProviderSpecific.

Now, let’s see how you can use the Profile object.

Profile.User.FirstName = "Andy";Profile.User.LastName = "Johnson";

As you can see it is very similar to the approach that we were already using. One important question that we need to ask our self is that when is the Profile object updated when we are using an entity class.

You might be thinking that since we can any type to be used for the properties then we can also use WebControls.Image types. Let’s see this in action:

<anonymousIdentificationenabled="true"/><profiledefaultProvider="MyProfileProvider">

<providers>

<addname="MyProfileProvider"type="System.Web.Profile.SqlProfileProvider"connectionStringName="ConnectionString"/>

</providers>

<properties>

<addname="User"type="

System.Web.UI.WebControls.Image 

"serializeAs="Binary"allowAnonymous="true"/> 

</properties>

</profile>

When you used the Image type in your code and assign it anything it will throw an exception. The reason is because Image class is not serializable and for the Profile properties to work the class must be serializable. You can off course save the image path as a string and later retrieve the string using the path.

When is the Profile Object Updated?

The data is updated in the database whenever we assign something to the Profile Object and when the request is completed. The data will only be updated in the database after the request has been completed.

Migrating Anonymous Profiles:

The last thing that I will discuss in this article is migrating the anonymous profiles to authenticated profiles. When ever the user comes to the website he always has his anonymous profile in which the username is created using GUID. You can consider a situation that a user visits the shopping cart application and after all the shopping he is asked for the username and password. This means that all that time the user was shopping using anonymous authentication. You can easily migrate user’s anonymous Profile by using the following code in Global.asax file.

void Profile_MigrateAnonymous(Object s, ProfileMigrateEventArgs e){

ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);

Profile.Name = anonProfile.Name;

Profile.PicturePath = anonProfile.PicturePath;

}

In the next article we will see how to make a Custom Profile Provider.

I hope you liked the article, happy programming!