The following code example shows a Web.config file that specifies a user profile that contains a ZipCode property of type string and a RecentSearchList property of type StringCollection. The generated Profile property of the current HttpContext will have strongly typed accessors for each of the specified properties.
<configuration>
<system.web>
<anonymousIdentification enabled="true" />
<properties>
<add name="ZipCode" allowAnonymous="true" />
<add name="RecentSearchList"
type="System.Collections.Specialized.StringCollection"
serializeAs="Xml"
allowAnonymous="true" />
</properties>
</system.web>
</configuration>
The following code example shows an ASP.NET page that reads and sets the ZipCode property specified for the user profile. Before attempting to run this code, set the provider to the default AspNetSqlProvider in the ASP.NET configuration settings for the Web site.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public Sub Page_PreRender()
If Profile.ZipCode = Nothing Then
PersonalizePanel.Visible = False
GetZipCodePanel.Visible = True
Else
ZipCodeLabel.Text = Profile.ZipCode
' Get personalized information for zip code here.
PersonalizePanel.Visible = True
GetZipCodePanel.Visible = False
End If
End Sub
Public Sub ChangeZipCode_OnClick(sender As Object, args As EventArgs)
ZipCodeTextBox.Text = Profile.ZipCode
Profile.ZipCode = Nothing
PersonalizePanel.Visible = False
GetZipCodePanel.Visible = True
End Sub
Public Sub EnterZipCode_OnClick(sender As Object, args As EventArgs)
Profile.ZipCode = ZipCodeTextBox.Text
End Sub
</script>
<html >
<head>
<title>Home Page</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
<!-- Information for Zip Code here. -->
<br />
<asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
OnClick="ChangeZipCode_OnClick" />
</asp:Panel>
<asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
You can personalize this page by entering your Zip Code:
<asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
<asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
OnClick="EnterZipCode_OnClick" />
</asp:Panel>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void Page_PreRender()
{
if (Profile.ZipCode == null)
{
PersonalizePanel.Visible = false;
GetZipCodePanel.Visible = true;
}
else
{
ZipCodeLabel.Text = Profile.ZipCode;
// Get personalized information for zip code here.
PersonalizePanel.Visible = true;
GetZipCodePanel.Visible = false;
}
}
public void ChangeZipCode_OnClick(object sender, EventArgs args)
{
ZipCodeTextBox.Text = Profile.ZipCode;
Profile.ZipCode = null;
PersonalizePanel.Visible = false;
GetZipCodePanel.Visible = true;
}
public void EnterZipCode_OnClick(object sender, EventArgs args)
{
Profile.ZipCode = ZipCodeTextBox.Text;
}
</script>
<html >
<head>
<title>Home Page</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
<!-- Information for Zip Code here. -->
<br />
<asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
OnClick="ChangeZipCode_OnClick" />
</asp:Panel>
<asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
You can personalize this page by entering your Zip Code:
<asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
<asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
OnClick="EnterZipCode_OnClick" />
</asp:Panel>
</td>
</tr>
</table>
</form>
</body>
</html>
The following code example defines a class that inherits from the ProfileBase class to create a custom profile. The type of the custom profile is specified in the inherits attribute of the profile configuration element in the Web.config file for an application.
Security Note: |
|---|
This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview. |
Imports System
Imports System.Web.Profile
Namespace Samples.AspNet.Profile
Public Class EmployeeProfile
Inherits ProfileBase
<SettingsAllowAnonymous(False)> _
<ProfileProvider("EmployeeInfoProvider")> _
Public Property Department As String
Get
Return MyBase.Item("EmployeeDepartment").ToString()
End Get
Set
MyBase.Item("EmployeeDepartment") = value
End Set
End Property
<SettingsAllowAnonymous(False)> _
<ProfileProvider("EmployeeInfoProvider")> _
Public Property Details As EmployeeInfo
Get
Return CType(MyBase.Item("EmployeeInfo"), EmployeeInfo)
End Get
Set
MyBase.Item("EmployeeInfo") = value
End Set
End Property
End Class
Public Class EmployeeInfo
Public Name As String
Public Address As String
Public Phone As String
Public EmergencyContactName As String
Public EmergencyContactAddress As String
Public EmergencyContactPhone As String
End Class
End Namespace
using System;
using System.Web.Profile;
namespace Samples.AspNet.Profile
{
public class EmployeeProfile : ProfileBase
{
[SettingsAllowAnonymous(false)]
[ProfileProvider("EmployeeInfoProvider")]
public string Department
{
get { return base["EmployeeDepartment"].ToString(); }
set { base["EmployeeDepartment"] = value; }
}
[SettingsAllowAnonymous(false)]
[ProfileProvider("EmployeeInfoProvider")]
public EmployeeInfo Details
{
get { return (EmployeeInfo)base["EmployeeInfo"]; }
set { base["EmployeeInfo"] = value; }
}
}
public class EmployeeInfo
{
public string Name;
public string Address;
public string Phone;
public string EmergencyContactName;
public string EmergencyContactAddress;
public string EmergencyContactPhone;
}
}