FindProfilesByUserName Method (ProfileAuthenticationOption, String)
Collapse the table of content
Expand the table of content

ProfileManager.FindProfilesByUserName Method (ProfileAuthenticationOption, String)


Retrieves all profile information for profiles in which the user name matches the specified name.

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

public static ProfileInfoCollection FindProfilesByUserName(
	ProfileAuthenticationOption authenticationOption,
	string usernameToMatch


Type: System.Web.Profile.ProfileAuthenticationOption

One of the ProfileAuthenticationOption enumeration values, specifying whether anonymous, authenticated, or both types of profiles are returned.

Type: System.String

The user name for which to search.

Return Value

Type: System.Web.Profile.ProfileInfoCollection

A ProfileInfoCollection containing user profile information for profiles where the user name matches the supplied usernameToMatch parameter.

Exception Condition

usernameToMatch is null.


usernameToMatch is an empty string ("").

The FindProfilesByUserName method returns a list of all profile information for which the user name matches the supplied usernameToMatch parameter value for the configured ApplicationName property value. Use the authenticationOption parameter to specify whether you want only anonymous profiles, only authenticated profiles, or all profiles to be searched.

You can retrieve profile information for user profiles in pages of data using the overload for the FindProfilesByUserName method that takes additional pageIndex and pageSize parameters.

Note that your profile provider may support wildcard characters in the usernameToMatch parameter. For example, the FindProfilesByUserName method of the SqlProfileProvider class supports wildcard characters in the usernameToMatch parameter.

The FindProfilesByUserName method calls the FindProfilesByUserName method of the default profile provider. The default profile provider is specified using the defaultProvider attribute of the configuration element. If the user profile contains properties that are managed by a profile provider other than the default provider, the data source for the other profile provider is not searched. To find profiles that are managed by a profile provider other than the default provider, obtain a reference to the profile provider using the Providers property and call the FindProfilesByUserName method of the provider directly.

The following code example allows a user to supply a usernameToMatch value and a ProfileAuthenticationOption enumeration value and retrieves profile information for any matching profiles. The example allows the user to specify whether to return inactive profiles exclusively. If the user specifies only inactive profiles, the FindInactiveProfilesByUserName method is called; otherwise, the FindProfilesByUserName method is called.

System_CAPS_security 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.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Profile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

ProfileAuthenticationOption authOption;

int inactiveDays = 90;

public void Page_Load()
  InactiveDaysLabel.Text = inactiveDays.ToString();
  authOption = GetAuthenticationOption();

  InactiveProfilesLabel.Text = ProfileManager.GetNumberOfInactiveProfiles(authOption,
                                 DateTime.Now.Subtract(new TimeSpan(inactiveDays, 0, 0, 0))).ToString();

  DeletedMessage.Text = "";

private ProfileInfoCollection GetProfiles()
  ProfileInfoCollection profiles;

  if (ShowInactiveCheckBox.Checked)
    profiles = ProfileManager.FindInactiveProfilesByUserName(authOption, 
                 DateTime.Now.Subtract(new TimeSpan(inactiveDays, 0, 0, 0)));
    profiles = ProfileManager.FindProfilesByUserName(authOption, 

  return profiles;

private void ShowProfiles()
  if (UserNameTextBox.Text.Trim() == "")
    Msg.Text = "Please specify a user name.";
    ProfileGrid.Visible = false;

  Msg.Text = "";
  ProfileGrid.Visible = true;

  ProfileGrid.DataSource = GetProfiles();

public void GoButton_OnClick(object sender, EventArgs args)

public void ShowInactiveCheckBox_OnCheckedChanged(object sender, EventArgs args)

public void DeleteButton_OnClick(object sender, EventArgs args)
  DeletedMessage.Text = ProfileManager.DeleteProfiles(GetProfiles()).ToString() + 
                          " profiles deleted.";

public void AuthenticationOptionListBox_OnSelectedIndexChanged(object sender, EventArgs args)
  authOption = GetAuthenticationOption();

private ProfileAuthenticationOption GetAuthenticationOption()
  if (AuthenticationOptionListBox.SelectedItem != null)
    switch (AuthenticationOptionListBox.SelectedItem.Value)
      case "Anonymous":
        return ProfileAuthenticationOption.Anonymous;
      case "Authenticated":
        return ProfileAuthenticationOption.Authenticated;
        return ProfileAuthenticationOption.All;

  return ProfileAuthenticationOption.All;

<html xmlns="" >
<title>Sample: Find Profiles</title>

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

  <asp:Label id="Msg" runat="Server" ForeColor="red" /><br />

  <table border="0" cellpadding="3" cellspacing="3">
      <td valign="top">UserName to Search for:</td>
      <td valign="top" colspan="2">
        <asp:TextBox id="UserNameTextBox" runat="server" />
        <asp:Button id="GoButton" Text=" Go " OnClick="GoButton_OnClick" runat="server" /><br />
      <td valign="top">Authentication Option</td>
      <td valign="top"><asp:ListBox id="AuthenticationOptionListBox" rows="3" runat="Server"
                         <asp:ListItem value="All" selected="True">All</asp:ListItem>
                         <asp:ListItem value="Authenticated">Authenticated</asp:ListItem>
                         <asp:ListItem value="Anonymous">Anonymous</asp:ListItem>
      <td valign="top"><asp:CheckBox id="ShowInactiveCheckBox" Checked="false" 
                                     AutoPostBack="true" runat="server"
                                     OnCheckedChanged="ShowInactiveCheckBox_OnCheckedChanged" />
                       Show profiles inactive for
                       <asp:Label id="InactiveDaysLabel" runat="server" />
                       days only.<br />
                       There are <asp:Label id="InactiveProfilesLabel" runat="server" />
                       inactive profiles.
      <td><asp:Button id="DeleteButton" runat="server" 
                      Text="Delete Profiles" OnClick="DeleteButton_OnClick" />
          <br /><i>(based on search results)</i>
      <td valign="top"><asp:Label id="DeletedMessage" runat="server" /></td>

  <asp:DataGrid id="ProfileGrid" runat="server"
                CellPadding="2" CellSpacing="1"
    <HeaderStyle BackColor="darkblue" ForeColor="white" />



.NET Framework
Available since 2.0
Return to top
© 2016 Microsoft