Export (0) Print
Expand All

ProfileManager Class

Manages user profile data and settings.

System.Object
  System.Web.Profile.ProfileManager

Namespace:  System.Web.Profile
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public NotInheritable Class ProfileManager

The ProfileManager type exposes the following members.

  NameDescription
Public propertyStatic memberApplicationNameGets or sets the name of the application for which to store and retrieve profile information.
Public propertyStatic memberAutomaticSaveEnabledGets a value indicating whether the user profile will be automatically saved at the end of the execution of an ASP.NET page.
Public propertyStatic memberEnabledGets a value indicating whether the user profile is enabled for the application.
Public propertyStatic memberProviderGets a reference to the default profile provider for the application.
Public propertyStatic memberProvidersGets a collection of the profile providers for the ASP.NET application.
Top

  NameDescription
Public methodStatic memberAddDynamicProfilePropertyAdds a profile property programmatically.
Public methodStatic memberDeleteInactiveProfilesDeletes user profile data for which the last activity date and time occurred before the specified date and time.
Public methodStatic memberDeleteProfileDeletes the profile for the specified user name from the data source.
Public methodStatic memberDeleteProfiles(ProfileInfoCollection)Deletes profile properties and information from the data source for the supplied list of profiles.
Public methodStatic memberDeleteProfiles(String())Deletes profile properties and information for the supplied list of user names.
Public methodStatic memberFindInactiveProfilesByUserName(ProfileAuthenticationOption, String, DateTime)Retrieves profile information for all profiles in which the last activity date occurred on or before the specified date and time and the user name for the profile matches the specified name.
Public methodStatic memberFindInactiveProfilesByUserName(ProfileAuthenticationOption, String, DateTime, Int32, Int32, Int32)Retrieves profile information in pages of data for profiles in which the last activity date occurred on or before the specified date and time and the user name for the profile matches the specified name.
Public methodStatic memberFindProfilesByUserName(ProfileAuthenticationOption, String)Retrieves all profile information for profiles in which the user name matches the specified name.
Public methodStatic memberFindProfilesByUserName(ProfileAuthenticationOption, String, Int32, Int32, Int32)Retrieves profile information in pages of data for profiles in which the user name matches the specified name.
Public methodStatic memberGetAllInactiveProfiles(ProfileAuthenticationOption, DateTime)Retrieves all user profile data for profiles in which the last activity date occurred on or before the specified date and time.
Public methodStatic memberGetAllInactiveProfiles(ProfileAuthenticationOption, DateTime, Int32, Int32, Int32)Retrieves a page of ProfileInfo objects for user profiles in which the last activity date occurred on or before the specified date and time.
Public methodStatic memberGetAllProfiles(ProfileAuthenticationOption)Retrieves user profile data for profiles in the data source.
Public methodStatic memberGetAllProfiles(ProfileAuthenticationOption, Int32, Int32, Int32)Retrieves pages of user profile data.
Public methodStatic memberGetNumberOfInactiveProfilesGets the number of profiles in which the last activity date occurred on or before the specified date.
Public methodStatic memberGetNumberOfProfilesGets the number of profiles in the data source.
Top

The ASP.NET profile is used to store and retrieve user settings in a data source such as a database. The user profile is accessed using the Profile property of the current HttpContext. Profile information and property values are managed using a profile provider.

The ProfileManager class is used to manage profile settings, search for user profiles, and delete user profiles that are no longer in use. The ProfileManager class provides static methods and properties that can be accessed by referencing the ProfileManager class in your application code. For an example, see the Example section in this topic and additional examples in the topics for members of the ProfileManager class.

By default, the user profile is disabled for all ASP.NET applications. To enable the user profile, set the enabled attribute of the profile configuration element to true, as shown in the following example.

<configuration>
  <system.web>
    <profile enabled="true" />
  </system.web>
</configuration>

For more information about the user profile, see ASP.NET Profile Properties Overview.

A profile provider is used to store and retrieve properties in the user profile. The .NET Framework includes a SqlProfileProvider class that stores user profile properties in a SQL Server database. A SqlProfileProvider instance named AspNetSqlProvider is specified in the machine configuration as the default profile provider. The AspNetSqlProvider instance connects to the SQL Server database on the local Web server. You can specify a SqlProfileProvider that connects to another SQL Server as the default profile provider by using the providers configuration element and the defaultProvider attribute of the profile configuration element, as shown in the following example.

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString=
      "Data Source=MySqlServer;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
  </connectionStrings>
  <system.web>
    <profile defaultProvider="SqlProvider">
      <providers>
        <clear />
        <add name="SqlProvider"
          type="System.Web.Profile.SqlProfileProvider"
          connectionStringName="SqlServices"
          applicationName="MyApplication" />
      </providers>
    </profile>
  </system.web>
</configuration>

The following code example shows an ASP.NET page that uses the ProfileManager class to manage inactive profiles.

Security noteSecurity 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.


<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Profile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Dim pageSize As Integer = 5
Dim totalProfiles As Integer 
Dim totalPages As Integer
Dim currentPage As Integer = 1

Dim authOption As ProfileAuthenticationOption
Dim inactiveDays As Integer = 120
Dim deletedProfiles As Integer = 0

Public Sub Page_Load()

  DeletedMessage.Text = ""

  authOption = GetAuthenticationOption()

  If Not IsPostBack Then  
    InactiveDaysTextBox.Text = inactiveDays.ToString()

    GetProfiles()
  Else
    inactiveDays = Convert.ToInt32(InactiveDaysTextBox.Text)
  End If
End Sub

Public Sub ProfileGrid_Delete(sender As Object, args As GridViewCommandEventArgs)
  ' Retrieve user name selected.

  Dim index As Integer = Convert.ToInt32(args.CommandArgument)

  Dim username As String = ProfileGrid.Rows(index).Cells(0).Text

  ProfileManager.DeleteProfiles(New string() {username})

  DeletedMessage.Text = "1 profile deleted."

  ' Refresh profile list.

  currentPage = Convert.ToInt32(CurrentPageLabel.Text)
  GetProfiles()
End Sub


Private Sub GetProfiles()
  ProfileGrid.DataSource = ProfileManager.GetAllInactiveProfiles(authOption, _
                               DateTime.Now.Subtract(New TimeSpan(inactiveDays, 0, 0, 0)), _
                               currentPage - 1, pageSize, totalProfiles)

  TotalProfilesLabel.Text = totalProfiles.ToString()

  totalPages = ((totalProfiles - 1) \ pageSize) + 1

  ' Ensure that we do not navigate past the last page of Profiles.

  If currentPage > totalPages Then  
    currentPage = totalPages
    GetProfiles()
    Return
  End If

  ProfileGrid.DataBind()
  CurrentPageLabel.Text = currentPage.ToString()
  TotalPagesLabel.Text = totalPages.ToString()

  If currentPage = totalPages Then
    NextButton.Visible = False
  Else
    NextButton.Visible = True
  End If

  If currentPage = 1 Then
    PreviousButton.Visible = False
  Else
    PreviousButton.Visible = True
  End If

  If totalProfiles <= 0 Then
    NavigationPanel.Visible = False
  Else
    NavigationPanel.Visible = True
  End If
End Sub

Public Sub NextButton_OnClick(sender As Object, args As EventArgs)
  currentPage = Convert.ToInt32(CurrentPageLabel.Text)
  currentPage += 1
  GetProfiles()
End Sub

Public Sub PreviousButton_OnClick(sender As Object, args As EventArgs)
  currentPage = Convert.ToInt32(CurrentPageLabel.Text)
  currentPage -= 1
  GetProfiles()
End Sub

Public Sub ModifyInactiveDaysButton_OnClick(sender As Object, args As EventArgs)
  GetProfiles()
End Sub

Public Sub AuthenticationOptionListBox_OnSelectedIndexChanged(sender As Object, args As EventArgs)
  authOption = GetAuthenticationOption()
  GetProfiles()
End Sub

Private Function GetAuthenticationOption() As ProfileAuthenticationOption 
  If Not AuthenticationOptionListBox.SelectedItem Is Nothing Then  
    Select Case AuthenticationOptionListBox.SelectedItem.Value
      Case "Anonymous"
        Return ProfileAuthenticationOption.Anonymous
      Case "Authenticated"
        return ProfileAuthenticationOption.Authenticated
      Case Else
        Return ProfileAuthenticationOption.All
    End Select
  End If

  Return ProfileAuthenticationOption.All
End Function

Public Sub DeleteAllInactiveButton_OnClick(sender As Object, args As EventArgs)
  deletedProfiles = ProfileManager.DeleteInactiveProfiles(authOption, _
                        DateTime.Now.Subtract(new TimeSpan(inactiveDays, 0, 0 ,0)))
  DeletedMessage.Text = deletedProfiles.ToString() & " profiles deleted."
  GetProfiles()
End SUb

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Find Profiles</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Profile List</h3>

  <table border="0" cellpadding="3" cellspacing="3">
    <tr>
      <td valign="top">Authentication Option</td>
      <td valign="top"><asp:ListBox id="AuthenticationOptionListBox" rows="3" runat="Server"
                                    AutoPostBack="True"
                                    OnSelectedIndexChanged="AuthenticationOptionListBox_OnSelectedIndexChanged">
                         <asp:ListItem value="All" selected="True">All</asp:ListItem>
                         <asp:ListItem value="Authenticated">Authenticated</asp:ListItem>
                         <asp:ListItem value="Anonymous">Anonymous</asp:ListItem>
                       </asp:ListBox>
      </td>
    </tr>
    <tr>
      <td valign="top" style="width:160">
        Number of Days for Profile to be considered "inactive"</td>
      <td valign="top" style="width:200">
        <asp:TextBox id="InactiveDaysTextBox" runat="Server" MaxLength="3" Columns="3" />
        <asp:Button id="ModifyInactiveDaysButton" runat="server" Text="Refresh Results" 
           OnClick="ModifyInactiveDaysButton_OnClick" /><br />
        <asp:Button id="DeleteAllInactiveButton" runat="Server"
           Text="Delete All Inactive Profiles" OnClick="DeleteAllInactiveButton_OnClick" />
      </td>
      <td valign="top">
        <asp:RequiredFieldValidator id="InactiveDaysRequiredValidator" runat="server"
           ControlToValidate="InactiveDaysTextBox" ForeColor="red"
           Display="Static" ErrorMessage="Required" />
        <asp:RegularExpressionValidator id="InactiveDaysValidator" runat="server"
           ControlToValidate="InactiveDaysTextBox" ForeColor="red"
           Display="Static" ValidationExpression="[0-9]*" 
           ErrorMessage="Inactive Days must be a whole number less than 1000 (e.g. 30, 120)" />
     </td>
    </tr>
    <tr>
      <td><asp:Label id="DeletedMessage" runat="server" /></td>
      <td><asp:Label id="TotalProfilesLabel" runat="server" /> inactive profiles found.</td>
    </tr>
  </table>

    <asp:Panel id="NavigationPanel" Visible="False" runat="server">
    <table border="0" cellpadding="3" cellspacing="3">
      <tr>
        <td style="width:100">Page <asp:Label id="CurrentPageLabel" runat="server" />
            of <asp:Label id="TotalPagesLabel" runat="server" /></td>
        <td style="width:60"><asp:LinkButton id="PreviousButton" Text="< Prev"
                            OnClick="PreviousButton_OnClick" runat="server" /></td>
        <td style="width:60"><asp:LinkButton id="NextButton" Text="Next >"
                            OnClick="NextButton_OnClick" runat="server" /></td>
      </tr>
    </table>
  </asp:Panel>

  <asp:GridView id="ProfileGrid" runat="server" AutoGenerateColumns="False"
                OnRowCommand="ProfileGrid_Delete"
                CellPadding="2" CellSpacing="1" Gridlines="None">
    <HeaderStyle BackColor="darkblue" ForeColor="white" />
    <Columns>
      <asp:BoundField HeaderText="User Name" DataField="Username" />
      <asp:BoundField HeaderText="Is Anonymous" DataField="IsAnonymous" />
      <asp:BoundField HeaderText="Last Updated" DataField="LastUpdatedDate" />
      <asp:BoundField HeaderText="Last Activity" DataField="LastActivityDate" />
      <asp:ButtonField HeaderText="Action" Text="Delete" ButtonType="Link" />
    </Columns>
  </asp:GridView>

</form>

</body>
</html>


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft