2. Defining a User Profile Within Web.config
As mentioned, a user profile is defined within a Web.config
file. The really nifty aspect of this approach is that you can interact
with this profile in a strongly typed manner using the inherited Profile property in your code files. To illustrate this, create a new Empty Web Site named FunWithProfiles, add a new *.aspx file, and open your Web.config file for editing.
Our goal is to make a profile which models the address start of track of the users who are in session, as well as all the number of times where they announced to this site. Not surprisingly, profile data is
defined within a <profile> element using a set of name/data type pairs. Consider the following profile, which is created within the scope of the <system.web> element:
<profile>
<properties>
<add name="StreetAddress" type="System.String" />
<add name="City" type="System.String" />
<add name="State" type="System.String" />
<add name="TotalPost" type="System.Int32" />
</properties>
</profile>
Here, we have specified a name and CLR data type for
each item in the profile (of course, we could add additional items for
zip code, name, and so forth, but I am sure you get the idea). Strictly
speaking, the type attribute is optional; however, the default is a System.String.
As you would guess, there are many other attributes that can be
specified in a profile entry to further qualify how this information
should be persisted in ASPNETDB.mdf. Table 1 illustrates some of the core attributes.
Table 1. Select Attributes of Profile Data
Attribute | Example Values | Meaning in Life |
---|
allowAnonymous | True | False | Restricts or allows anonymous access to this value. If it is set to false, anonymous users won't have access to this profile value. |
defaultValue | String | The value to return if the property has not been explicitly set. |
Name | String | A unique identifier for this property. |
Provider | String | The provider used to manage this value. It overrides the defaultProvider setting in Web.config or machine.config. |
readOnly | True | False | Restricts or allows write access (the default is false, i.e. it's not read-only). |
serializeAs | String | XML | Binary | The format of a value when persisting in the data store. |
type | Primitive | User-defined type | A .NET primitive type or class. Class names must be fully qualified (e.g., MyApp.UserData.ColorPrefs). |
We will see some of these attributes in action as we
modify the current profile. For now, let's see how to access this data
programmatically from within our pages.