3. Accessing Profile Data Programmatically
Recall that the whole purpose of the ASP.NET Profile
API is to automate the process of writing data to (and reading data
from) a dedicated database. To test this out for yourself, update the
UI of your Default.aspx file with a set of TextBoxes (and descriptive Labels) to gather the street address, city, and state of the user. As well, add a Button (named btnSubmit) and a final Label (named lblUserData) that will be used to display the persisted data, as shown in Figure 1.
Now, within the Click event handler of the button, make use of the inherited Profile property to persist each point of profile data based on what the user has entered in the related TextBox. Once you have persisted each piece of data within ASPNETDB.mdf, read each piece of data out of the database and format it into a string that is displayed on the lblUserData Label type. Finally, handle the page's Load event, and display the same information on the Label type. In this way, when users come to the page, they can see their current settings. Here is the complete code file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetUserAddress();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Database writes happening here!
Profile.StreetAddress = txtStreetAddress.Text;
Profile.City = txtCity.Text;
Profile.State = txtState.Text;
// Get settings from database.
GetUserAddress();
}
private void GetUserAddress()
{
// Database reads happening here!
lblUserData.Text = String.Format("You live here: {0}, {1}, {2}",
Profile.StreetAddress, Profile.City, Profile.State);
}
}
Now if you run this page, you will notice a lengthy delay the first time Default.aspx is requested. The reason is that the ASPNETDB.mdf
file is being created on the fly and placed within your App_Data
folder. You can verify this for yourself by refreshing Solution
Explorer (see Figure 2).
You will also find that the first time you come to this page, the lblUserData Label does not display any profile data, as you have not yet entered your data into the correct table of ASPNETDB.mdf. Once you enter values in the TextBox controls and post back to the server, you will find this Label is formatted with the persisted data.
Now, for the really interesting aspect of this
technology. If you shut down your browser and rerun your web site, you
will find that your previously entered profile data has indeed been
persisted, as the Label displays the correct information. This begs the obvious question, how were you remembered?
For this example, the Profile API used your Windows
network identity, which was obtained via your current machine
credentials. However, when you are building public web sites (where the
users are not part of a given domain), rest assured that the Profile
API integrates with the Forms-based authentication model of ASP.NET and
also supports the notion of "anonymous profiles," which allow you to
persist profile data for users who do not currently have an active
identity on your site.
NOTE
This edition of the text does not
address ASP.NET security topics (such as Forms-based authentication or
anonymous profiles). Consult the .NET Framework 4.0 SDK documentation
for details.