Click here to Skip to main content
Click here to Skip to main content
Go to top

ASP.NET Profile Provider

, 25 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Brief description of how to use the Profile Provider available in ASP.NET

Introduction

In the article, ASP.NET Membership and Role Provider, we have seen how to configure and use the in-built SQL Server Membership Provider. In this continuation article, we will see the usage of SqlProfileProvider. In any application, if you want to store the user information other than the Registration details like their address, user's interest, themes, etc., you can use the SqlProfileProvider. Now we start with the basics and we will see all possible ways to store user's information.

Implementing the SqlProfileProvider

The Profile provider can be configured to the application by using the following settings in the web.config file.

ProfileProvider Settings

The Profile provider settings are simple and straight forward, the elements include the name of the profile provider and the properties of the profile which are to be stored. You can also use the group property of the profile provider in order to the group the elements.

Web.Config settings

<profile defaultProvider="Demo_ProfileProvider">
<providers>
	<add name="Demo_ProfileProvider" connectionStringName="cnn"
	applicationName="/" type="System.Web.Profile.SqlProfileProvider,
	System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
	<add name="Name" type="String"/>
	<add name="DateofBirth" type="DateTime"/>
	<add name="Place" type="string"/>
	<add name="Languages" type="string"/>
	<add name="AboutMe" type="String"/>
	<add name="Employer" type="String"/>
	<add name="Project" type="String"/>
	<add name="Designation" type="String"/>
	<add name="University" type="String"/>
</properties>
</profile>    

Using the Profile Profile Properties to Save User Profiles

The following code is used to save the users profiles. Profile.Save() method updates the profile data source with changed profile property values.

public void SaveProfile()
   {
       Profile.Name = txtfullname.Text;
       Profile.DateofBirth = Convert.ToDateTime(txtdateofbirth.Text);
       Profile.AboutMe = txtaboutme.Text;
       Profile.Designation = txtdesignation.Text;
       Profile.Employer = txtemployer.Text;
       Profile.Languages = txtlanguages.Text;
       Profile.Place = txtplace.Text;
       Profile.Project = txtproject.Text;
       Profile.University = txtusername.Text;
       Profile.Save();
   }    

Using Profile Groups

Profile Groups help you to organize the user profile in a better manner. For example, in the above example, we can group the userinfo to ProfesssionalInfo and BasicInfo. Even in some, you can extend the information into multiple groups. Now we will see how to use groups for profile provider.

The following settings are applied to web.config in order to achieve Profile Groups.

Web.Config settings

<profile defaultProvider="Demo_ProfileProvider">
<providers>
	<add name="Demo_ProfileProvider" connectionStringName="cnn"
	applicationName="/" type="System.Web.Profile.SqlProfileProvider,
	System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
<properties>
   <group name ="BasicInfo">
       	<add name ="Name" type="String"/>
         <add name="DateofBirth" type="DateTime"/>
         <add name="BirthPlace" type="string"/>
         <add name="Place" type="string"/>
         <add name="Languages" type="string"/>
         <add name="AboutMe" type="String"/>
        </group>
        <group name ="ProfessionlProfile">
         <add name="Employer" type="String"/>
         <add name="Project" type="String"/>
         <add name="Designation" type="String"/>
         <add name="University" type="String"/>
        </group>
</properties>
</profile>	

Now you can save the profiles by using profile group properties as shown below. You will not find much difference between the code to save profiles by using groups with the one shown above.

/// <summary>
/// Save Profile using Groups.
/// </summary>

public void SaveProfile()
{
    Profile.BasicInfo.Name = txtfullname.Text;
    Profile.BasicInfo.DateofBirth = Convert.ToDateTime(txtdateofbirth.Text);
    Profile.BasicInfo.AboutMe = txtaboutme.Text;
    Profile.ProfessionlProfile.Designation = txtdesignation.Text;
    Profile.ProfessionlProfile.Employer = txtemployer.Text;
    Profile.BasicInfo.Languages = txtlanguages.Text;
    Profile.BasicInfo.BirthPlace = txtplace.Text;
    Profile.BasicInfo.Place = txtlivesin.Text;
    Profile.ProfessionlProfile.Project = txtproject.Text;
    Profile.ProfessionlProfile.University = txtusername.Text;
    Profile.Save();
}	

Using Custom Types

Using Custom Types with profiles is easy. You need to begin with creating a class that consists of all your required properties and then add the property to the profile provider settings that use it.

Initially, create a simple userinfo class as shown below and place it in your App_Code folder:

[Serializable()]
public class UserInfo
{
    #region variables
    private string name;
    private string dateofbirth;
    private string birthplace;
    private string place;
    private string languages;
    private string aboutme;
    private string employer;
    private string project;
    private string designation;
    private string university;
    #endregion variables

    #region Properties
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string DateofBirth
    {
        get { return dateofbirth; }
        set { dateofbirth = value; }
    }

    public string Languages
    {
        get { return languages; }
        set { languages = value; }
    }

    public string BirthPlace
    {
        get { return birthplace; }
        set { birthplace = value; }
    }

    public string Place
    {
        get { return place; }
        set { place = value; }
    }

    public string AboutMe
    {
        get { return aboutme; }
        set { aboutme = value; }
    }

    public string Employer
    {
        get { return employer; }
        set { employer = value; }
    }

    public string Project
    {
        get { return project; }
        set { project = value; }
    }

    public string Designation
    {
        get { return designation; }
        set { designation = value; }
    }

    public string University
    {
        get { return university; }
        set { university = value; }
    }

    #endregion Properties

    #region Constructors
    public UserInfo()
    {
	//
	// TODO: Add constructor logic here
	//
    }

    public UserInfo(string _name, string _dateofbirth, string _birthplace,
                   string _place, string _languages, string _aboutme,
                   string _employer,string _project, string _designation, 
			string _university)
    {
        name = _name;
        dateofbirth = _dateofbirth;
        birthplace = _birthplace;
        place = _place;
        languages = _languages;
        aboutme = _aboutme;
        employer = _employer;
        project = _project;
        designation = _designation;
        university = _university;
    }
    #endregion Constructors
}

The next step to add property that uses it as shown below:

 <add name ="UserInfo" type ="UserInfo"/>

Finally update the profiles by using single line code like this:

/// <summary>
/// Save Profiles using Custom Types.
/// </summary>

public void saveprofile()
{
    Profile.UserInfo = new UserInfo(txtusername.Text, txtdateofbirth.Text, txtplace.Text,
                                    	txtlivesin.Text, txtlanguages.Text, txtaboutme.Text,
                                    	txtemployer.Text,txtproject.Text, 
				txtdesignation.Text, txtcollege.Text);
    Profile.Save();
}

Retrieving User's Profile

By this way, we are done with saving the user's profile to the data store. Now we'll see how to retrieve the stored user's information and display them back to the user. The following code shows you the way to achieve this. The code is very simple and straight forward. Simply create a page for the user and then add the below code to that page.

StringBuilder text = new StringBuilder();
string s = Profile.Designation + "at" + Profile.Employer + "Lives in" +
                   Profile.Place + "From" + Profile.BirthPlace;
text.Append(s);
Literal1.Text = text.ToString();

Working with Anonymous Users

So till now, we have worked with profile data for the registered users. What if you want to allow unregistered users to save their information on your site or in another way. If you want the unregistered users information, then we need to change the settings. ASP.NET Profile Provider provides you the feature to work with the anonymous users.

The basic idea behind the feature is that the anonymous identification feature automatically generates a random identifier for any anonymous user. This random identifier (also called as GUID) stores the user profiles data in the data store, even though user id in unavailable. The userid is present in the browser cookies or if the cookies are disabled, it is available on the URL. Once you close and open the browser, the anonymous session is ended and a new session is created. The following settings are used to use anonymous...

<anonymousIdentification  enabled="true"/>

...and then you set allowAnonymous property to true as shown below. You can also restrict some properties for registered users or you can make all properties available to the anonymous users.

<properties>
	<add name="Name" type="String" allowAnonymous="true"/>
	<add name="DateofBirth" type="DateTime"  allowAnonymous="true"/>
	<add name="BirthPlace" type="string"  allowAnonymous="true"/>
    	<add name="Place" type="string"  allowAnonymous="true"/>
	<add name="Languages" type="string"  allowAnonymous="true"/>
	<add name="AboutMe" type="String"  allowAnonymous="true"/>
	<add name="Employer" type="String"  allowAnonymous="true"/>
	<add name="Project" type="String"  allowAnonymous="true"/>
	<add name="Designation" type="String"  allowAnonymous="true"/>
	<add name="University" type="String"  allowAnonymous="true"/>
</properties>

Now anonymous user's login can migrate the user's information. In this section, we will see how we can achieve this task.

Start with adding a simple event handler to your global.asax file.

void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
{
        ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);

        if (anonProfile.UserInfo.Name != null || anonProfile.UserInfo.Name != "")
        {
            Profile.UserInfo = anonProfile.UserInfo;
        }

        System.Web.Profile.ProfileManager.DeleteProfile(pe.AnonymousID);

        AnonymousIdentificationModule.ClearAnonymousIdentifier();
}

In this article, I have tried to cover the sections related to the ASP.NET Profile provider. Hope this has helped you to understand the basic concepts of how to user profile provider in your ASP.NET application. Any other updates would be given in the further revised versions.

Points of Interest

In order to save the profiles of user, you can customize the Create User Wizard control or you can use custom create user wizard. One type of create user wizard which is developed using JavaScript is attached with the source code. You can find more information about the create user wizard in the following article, JavaScript Create User Wizard for ASP.NET.

Conclusion

In this article, we have seen a brief description about how to implement Profile provider and how we can use the profile properties in our application. You can find some more information about profile provider and their properties at the below MSDN links.

External Resources

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

S V Saichandra
Software Developer GSS Infotech Ltd.
India India
S V Sai Chandra is a Software Engineer from Hyderabad Deccan. He started Embedded Programing in his college days and now he is a Web Developer by Profession. He Loves coding and his passion is always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books, painting and hang out with friends is his most favorite past time hobby.
He blogs at
http://technowallet.blogspot.com
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 2 Pinmemberrushijoshi11-Nov-11 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 25 Nov 2011
Article Copyright 2011 by S V Saichandra
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid