ASP.NET Profile Properties 
This page is specific to:.NET Framework Version:
2.03.0
ASP.NET 
ASP.NET Profile Properties 

You can provide users of your Web site with a custom experience by defining and using profile properties. You can use profile properties to track any custom information your application requires, including:

  • User information, such as an address or city.

  • Preferences, such as a color scheme or list of stocks to follow.

  • Custom information about for the current session, such as a shopping cart.

Once you have defined profile properties, ASP.NET automatically associates individual instances of the profile properties with each user, and you can use code to set or get the values. ASP.NET persists property values in a data store (which you can configure), and the next time a user visits your site, ASP.NET automatically retrieves the profile property value for that user.

The topics in this section describe how profile properties work and how to create and manage them.

In This Section

Reference

Related Sections

ASP.NET State Management

Describes the ways provided by ASP.NET for storing information between page requests.

ASP.NET Web Parts Pages

Describes how to create pages with application features that users can select and customize in a browser.

Community Content

Working with ASP.NET Profiles and Anonymous Users
Added by:DeveloperHeadQuarters.com

I recently had to implement Profiles for our company and since I was new to VS 2005 Framework 2.0/3.0 and ‘Profiles’ particularly, I did what any good developer would…I Googled for help. After several hours of working through issues like creating and defining custom profiles, migrating anonymous user profiles, complex data types, etc…I thought I would break it down for others to spare them the frustration. I left out a lot…but, the bare bones is here and should help you get started. I don’t like talking (or typing) and I have to assume the rest of you are good developers too. You should be able to just look at what I did and get it. If not and your having trouble understanding I would recommend you check out some other links first like this one http://www.odetocode.com/Articles/440.aspx and then come back. I hope this helps...Remember, bare bones baby!

First edit your Web.config. mine will allow anonymous users to create a profile which I can migrate later. It also uses a custom type.

<anonymousIdentification enabled="true" />
<profile defaultProvider="SqlProvider" enabled="true">
<providers>
<clear/>
<add name="SqlProvider"
connectionStringName="conn"
applicationName="YourApplicationsName"
type="System.Web.Profile.SqlProfileProvider"/>
</providers>
<properties>
<add name="MemberProfile" allowAnonymous="true"
type="CustomProfile"/>
</properties>
</profile>

Then I created a custom Class file...

[Serializable()]
public class CustomProfile
{
private string _fName = string.Empty;
private string _lName = string.Empty;
private string _eMail = string.Empty;
private string _web = string.Empty;
private string _membercomment = string.Empty;
private string _occupation = string.Empty;
private string _phone = string.Empty;
private string _address = string.Empty;
private string _city = string.Empty;
private string _state = string.Empty;
private string _zip = string.Empty;
private string _photo = string.Empty;
private int _photosize = 0;
private string _age = string.Empty;
private string _hobbies = string.Empty;
private string _credentials = string.Empty;
private int _skilllevel = 0;
private string _options = string.Empty;

public CustomProfile()
{
//
// TODO: Add constructor logic here
//
}

public string FirstName
{
get
{
return _fName;
}
set
{
_fName = value;
}
}

public string LastName
{
get
{
return _lName;
}
set
{
_lName = value;
}
}

...etc

You can do all sorts of stuff...you may also want to derive from other base or custom classes

Then I create a page that will allow users to input values and store the profile properties like this:

protected void SaveProfile()
{
if (Profile.MemberProfile != null)
{
Profile.MemberProfile = new CustomProfile();
Hashtable hOption = new Hashtable();
Profile.MemberProfile.FirstName = Server.HtmlEncode(this.txtFirstName.Text);
Profile.MemberProfile.LastName = Server.HtmlEncode(this.txtLastName.Text);
Profile.MemberProfile.Email = Server.HtmlEncode(this.txtMail.Text);

etc....One item I want to show is how I saved a hash table of user options selected from several check boxes. This must be serialized and I convert to base 64 string so it can be saved as XML in dbo.aspnet_Profile PropertiesValueString field. NOTE: vEditProfile is the ID of my Multi-View control which contains all the checkbox controls.

foreach (Control ctrl in vEditProfile.Controls)
{
if (ctrl.GetType() == typeof(CheckBox))
{
CheckBox option = (CheckBox)ctrl;
if (option.Checked)
hOption.Add(option.ID, "True");
else
hOption.Add(option.ID, "False");
}
}

MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, hOption);
Byte[] byteOptions = ms.ToArray();
string encodedOptions = Convert.ToBase64String(byteOptions);
Profile.MemberProfile.ProfileOptions = encodedOptions;
ms.Close();

Now when getting the profiles I did this...

protected void UsersProfile()
{
if (Profile.MemberProfile != null)
{
this.txtFirstName.Text = Server.HtmlDecode(Profile.MemberProfile.FirstName);
this.txtLastName.Text = Server.HtmlDecode(Profile.MemberProfile.LastName);
this.txtMail.Text = Server.HtmlDecode(Profile.MemberProfile.Email);
this.txtWeb.Text = Server.HtmlDecode(Profile.MemberProfile.Website);
this.txtOccupation.Text = Server.HtmlDecode(Profile.MemberProfile.Occupation);

string encodedOptions = Profile.MemberProfile.ProfileOptions;

...etc

and this for the hastable (complext data type)

Hashtable hOption = new Hashtable();

if (encodedOptions != null)
{
BinaryFormatter bf = new BinaryFormatter();
Byte[] byteOptions = Convert.FromBase64String(encodedOptions);
if (byteOptions.Length > 0)
{
MemoryStream ms = new MemoryStream(byteOptions);
hOption = (Hashtable)bf.Deserialize(ms);
ms.Close();
}
}

foreach (Control ctrl in vEditProfile.Controls)
{
if (ctrl.GetType() == typeof(CheckBox))
{
CheckBox option = (CheckBox)ctrl;
if (hOption[option.ID] != null)
{
option.Checked = Convert.ToBoolean(hOption[option.ID]);
}
}
}

Now lets look at the Global asax...

protected void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)
{
int HasProfile = ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated, User.Identity.Name).Count;
ProfileCommon tempProfile = Profile.GetProfile(e.AnonymousID);

if ((e.AnonymousID != string.Empty) && (HasProfile == 0))
{
Profile.MemberProfile.FirstName = tempProfile.MemberProfile.FirstName;
Profile.MemberProfile.LastName = tempProfile.MemberProfile.LastName;
Profile.MemberProfile.Email = tempProfile.MemberProfile.Email;

etc...Of course if you have lots of properties there is a better way to get and save all values but anyway...

The keys here are in the line:

int HasProfile = ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated, User.Identity.Name).Count;

I want to make sure no profile already exists so I don't overwrite a valid users existing profile. Else if it is an anonymous user they would not have an existing profile and I can migrate all the forms data for this AnonymousID into a new profile for this user. After that I will clean up regardless of it being an existing user or not..

if (User.Identity.IsAuthenticated)
{
Membership.DeleteUser(tempProfile.UserName, true);
AnonymousIdentificationModule.ClearAnonymousIdentifier();
}

K...thats it. I really tried to keep it simple. There is so much more to it. I welcome suggestions.

© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View