Membership.FindUsersByName 方法

定义

获取成员资格用户的集合,其中用户名包含要匹配的指定用户名。Gets a collection of membership users where the user name contains the specified user name to match.

重载

FindUsersByName(String)

获取成员资格用户的集合,其中用户名包含要匹配的指定用户名。Gets a collection of membership users where the user name contains the specified user name to match.

FindUsersByName(String, Int32, Int32, Int32)

获取一个成员资格用户的集合,并显示在一个数据页中,这些用户的用户名包含要匹配的指定用户名。Gets a collection of membership users, in a page of data, where the user name contains the specified user name to match.

FindUsersByName(String)

获取成员资格用户的集合,其中用户名包含要匹配的指定用户名。Gets a collection of membership users where the user name contains the specified user name to match.

public:
 static System::Web::Security::MembershipUserCollection ^ FindUsersByName(System::String ^ usernameToMatch);
public static System.Web.Security.MembershipUserCollection FindUsersByName (string usernameToMatch);
static member FindUsersByName : string -> System.Web.Security.MembershipUserCollection
Public Shared Function FindUsersByName (usernameToMatch As String) As MembershipUserCollection

参数

usernameToMatch
String

要搜索的用户名。The user name to search for.

返回

一个 MembershipUserCollection 集合,其中包含与 usernameToMatch 参数匹配的所有用户。A MembershipUserCollection that contains all users that match the usernameToMatch parameter.

删除 usernameToMatch 参数值的前导和尾随空格。Leading and trailing spaces are trimmed from the usernameToMatch parameter value.

异常

usernameToMatch 是一个空字符串。usernameToMatch is an empty string.

usernameToMatchnullusernameToMatch is null.

示例

下面的代码示例使用 FindUsersByName 方法基于用户输入检索成员资格数据库中的成员资格用户信息,并在数据页中显示结果。The following code example uses the FindUsersByName method to retrieve membership user information from the membership database based on user input and displays the results in pages of data.

重要

此示例包含一个文本框,该文本框接受用户输入,这是一个潜在的安全威胁。This example contains a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。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.Security" %>
<!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 GoButton_OnClick(object sender, EventArgs args)
{
  UserGrid.DataSource = Membership.FindUsersByName(UsernameTextBox.Text);
  UserGrid.DataBind();
}

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

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

  Username to Search for: 
    <asp:TextBox id="UsernameTextBox" runat="server" />
    <asp:Button id="GoButton" Text=" Go " OnClick="GoButton_OnClick" runat="server" /><br />

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

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!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 GoButton_OnClick(sender As Object, args As EventArgs)
  UserGrid.DataSource = Membership.FindUsersByName(UsernameTextBox.Text)
  UserGrid.DataBind()
End Sub

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

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

  Username to Search for: 
    <asp:TextBox id="UsernameTextBox" runat="server" />
    <asp:Button id="GoButton" Text=" Go " OnClick="GoButton_OnClick" runat="server" /><br />

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

</form>

</body>
</html>

注解

FindUsersByName 返回的成员资格用户的列表,其中的用户名与所配置 applicationName的提供 usernameToMatch 相匹配。FindUsersByName returns a list of membership users where the user name matches the supplied usernameToMatch for the configured applicationName.

SqlMembershipProvider 使用 LIKE 子句对 usernameToMatch 参数执行其搜索。The SqlMembershipProvider performs its search using a LIKE clause against the usernameToMatch parameter. LIKE 子句中 SQL Server 支持的任何通配符都可以在 usernameToMatch 参数值中使用。Any wildcards that are supported by SQL Server in LIKE clauses can be used in the usernameToMatch parameter value.

将从所有参数值中去除前导空格和尾随空格。Leading and trailing spaces are trimmed from all parameter values.

另请参阅

FindUsersByName(String, Int32, Int32, Int32)

获取一个成员资格用户的集合,并显示在一个数据页中,这些用户的用户名包含要匹配的指定用户名。Gets a collection of membership users, in a page of data, where the user name contains the specified user name to match.

public:
 static System::Web::Security::MembershipUserCollection ^ FindUsersByName(System::String ^ usernameToMatch, int pageIndex, int pageSize, [Runtime::InteropServices::Out] int % totalRecords);
public static System.Web.Security.MembershipUserCollection FindUsersByName (string usernameToMatch, int pageIndex, int pageSize, out int totalRecords);
static member FindUsersByName : string * int * int * int -> System.Web.Security.MembershipUserCollection
Public Shared Function FindUsersByName (usernameToMatch As String, pageIndex As Integer, pageSize As Integer, ByRef totalRecords As Integer) As MembershipUserCollection

参数

usernameToMatch
String

要搜索的用户名。The user name to search for.

pageIndex
Int32

要返回的结果页的索引。The index of the page of results to return. pageIndex 从零开始。pageIndex is zero-based.

pageSize
Int32

要返回的结果页的大小。The size of the page of results to return.

totalRecords
Int32

匹配用户的总数。The total number of matched users.

返回

包含一页 MembershipUserCollectionpageSize 对象的 MembershipUser,这些对象从 pageIndex 指定的页开始。A MembershipUserCollection that contains a page of pageSizeMembershipUser objects beginning at the page specified by pageIndex.

删除 usernameToMatch 参数值的前导和尾随空格。Leading and trailing spaces are trimmed from the usernameToMatch parameter value.

异常

usernameToMatch 是一个空字符串。usernameToMatch is an empty string.

-或--or- pageIndex 小于零。pageIndex is less than zero.

-或--or- pageSize 小于 1。pageSize is less than 1.

usernameToMatchnullusernameToMatch is null.

示例

下面的代码示例使用 FindUsersByName 方法基于用户输入检索成员资格数据库中的成员资格用户信息,并在数据页中显示结果。The following code example uses the FindUsersByName method to retrieve membership user information from the membership database based on user input and displays the results in pages of data.

重要

此示例包含一个文本框,该文本框接受用户输入,这是一个潜在的安全威胁。This example contains a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。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.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

int pageSize = 5;
int totalUsers;
int totalPages;
int currentPage = 1;

private void GetUsers()
{
  UserGrid.DataSource = Membership.FindUsersByName(UsernameTextBox.Text, 
                          currentPage - 1, pageSize, out totalUsers);
  totalPages = ((totalUsers - 1) / pageSize) + 1;

  // Ensure that we do not navigate past the last page of users.

  if (currentPage > totalPages)
  {
    currentPage = totalPages;
    GetUsers();
    return;
  }

  UserGrid.DataBind();
  CurrentPageLabel.Text = currentPage.ToString();
  TotalPagesLabel.Text = totalPages.ToString();

  if (currentPage == totalPages)
    NextButton.Visible = false;
  else
    NextButton.Visible = true;

  if (currentPage == 1)
    PreviousButton.Visible = false;
  else
    PreviousButton.Visible = true;

  if (totalUsers <= 0)
    NavigationPanel.Visible = false;
  else
    NavigationPanel.Visible = true;
}

public void NextButton_OnClick(object sender, EventArgs args)
{
  currentPage = Convert.ToInt32(CurrentPageLabel.Text);
  currentPage++;
  GetUsers();
}

public void PreviousButton_OnClick(object sender, EventArgs args)
{
  currentPage = Convert.ToInt32(CurrentPageLabel.Text);
  currentPage--;
  GetUsers();
}

public void GoButton_OnClick(object sender, EventArgs args)
{
  currentPage = 1;
  GetUsers();
}

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

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

  Username to Search for: 
    <asp:TextBox id="UsernameTextBox" runat="server" />
    <asp:Button id="GoButton" Text=" Go " OnClick="GoButton_OnClick" runat="server" /><br />

  <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:DataGrid id="UserGrid" runat="server"
                CellPadding="2" CellSpacing="1"
                Gridlines="Both">
    <HeaderStyle BackColor="darkblue" ForeColor="white" />
  </asp:DataGrid>

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!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 totalUsers As Integer
Dim totalPages As Integer
Dim currentPage As Integer = 1

Private Sub GetUsers()
  UserGrid.DataSource = Membership.FindUsersByName(UsernameTextBox.Text, _
                          currentPage - 1, pageSize, totalUsers)

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

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

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

  UserGrid.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 totalUsers <= 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
  GetUsers()
End Sub

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

Public Sub GoButton_OnClick(sender As Object, args As EventArgs)
  currentPage = 1
  GetUsers()
End Sub

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

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

  Username to Search for: 
    <asp:TextBox id="UsernameTextBox" runat="server" />
    <asp:Button id="GoButton" Text=" Go " OnClick="GoButton_OnClick" runat="server" /><br />

  <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:DataGrid id="UserGrid" runat="server"
                CellPadding="2" CellSpacing="1"
                Gridlines="Both">
    <HeaderStyle BackColor="darkblue" ForeColor="white" />
  </asp:DataGrid>

</form>

</body>
</html>

注解

FindUsersByName 返回的成员资格用户的列表,其中的用户名与所配置 applicationName的提供 usernameToMatch 相匹配。FindUsersByName returns a list of membership users where the user name matches the supplied usernameToMatch for the configured applicationName.

SqlMembershipProvider 使用 LIKE 子句对 usernameToMatch 参数执行其搜索。The SqlMembershipProvider performs its search using a LIKE clause against the usernameToMatch parameter. LIKE 子句中 SQL Server 支持的任何通配符都可以在 usernameToMatch 参数值中使用。Any wildcards that are supported by SQL Server in LIKE clauses can be used in the usernameToMatch parameter value.

FindUsersByName 返回的结果受 pageIndexpageSize 参数的约束。The results returned by FindUsersByName are constrained by the pageIndex and pageSize parameters. pageSize 参数标识 MembershipUserCollection中要返回的 MembershipUser 对象的最大数目。The pageSize parameter identifies the maximum number of MembershipUser objects to return in the MembershipUserCollection. pageIndex 参数标识要返回的结果页,其中0标识第一页。The pageIndex parameter identifies which page of results to return, where 0 identifies the first page. totalRecords 参数是一个 out 参数,设置为与 usernameToMatch 值匹配的成员资格用户的总数。The totalRecords parameter is an out parameter that is set to the total number of membership users that matched the usernameToMatch value. 例如,如果找到13个用户,其中 usernameToMatch 匹配的部分或整个用户名,并且 pageIndex 值为1且 pageSize 为5,则返回的 MembershipUserCollection 将包含返回的第六个到第10个用户。For example, if 13 users were found where usernameToMatch matched part of or the entire user name, and the pageIndex value was 1 with a pageSize of 5, the MembershipUserCollection returned would contain the sixth through the tenth users returned. totalRecords 将设置为13。totalRecords would be set to 13.

另请参阅

适用于