Share via


SqlRoleProvider.FindUsersInRole(String, String) 方法

定义

获取属于某个角色且与指定的用户名相匹配的用户名的数组。

public:
 override cli::array <System::String ^> ^ FindUsersInRole(System::String ^ roleName, System::String ^ usernameToMatch);
public override string[] FindUsersInRole (string roleName, string usernameToMatch);
override this.FindUsersInRole : string * string -> string[]
Public Overrides Function FindUsersInRole (roleName As String, usernameToMatch As String) As String()

参数

roleName
String

作为搜索范围的角色。

usernameToMatch
String

要搜索的用户名。

返回

String[]

一个字符串数组,包含用户名与 usernameToMatch 匹配且用户是指定角色的成员的所有用户的名称。

例外

roleNamenull(在 Visual Basic 中为 Nothing)。

- 或 -

usernameToMatchnull

roleName 为空字符串或者包含逗号。

- 或 -

usernameToMatch 是一个空字符串。

- 或 -

roleName 的长度超过 256 个字符。

- 或 -

usernameToMatch 的长度超过 256 个字符。

在数据库中找不到 roleName

- 或 -

与数据库进行通信时发生未知错误。

示例

下面的代码示例使用 FindUsersInRole 方法根据用户输入显示角色成员身份。 有关启用角色管理的 Web.config 文件的示例,请参阅 Roles

重要

此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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">

string[] users;

public void Page_Load()
{
  if (!IsPostBack)
  {
    RolesListBox.DataSource = Roles.GetAllRoles();
    RolesListBox.DataBind();
  }
}

public void GoButton_OnClick(object sender, EventArgs args)
{
  Msg.Text = "";
  users = null;

  if (RolesListBox.SelectedItem == null)
  {
    Msg.Text = "Please select a role.";
    return;
  }

  users = Roles.FindUsersInRole(RolesListBox.SelectedItem.Text, UsernameTextBox.Text);

  if (users.Length < 1)
  {
    Msg.Text = "No matching users found in selected role.";
  }

  UserGrid.DataSource = users;
  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>

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

  <table border="0" cellpadding="3" cellspacing="3">
    <tr>
      <td valign="top">Role:</td>
      <td valign="top"><asp:ListBox id="RolesListBox" runat="Server" /></td>
    </tr>
    <tr>
      <td valign="top">Username to Search for:</td>
      <td valign="top"><asp:TextBox id="UsernameTextBox" runat="server" /></td>
    </tr>
  </table>
  <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">

Dim users() As String

Public Sub Page_Load()
  If Not IsPostBack Then
    RolesListBox.DataSource = Roles.GetAllRoles()
    RolesListBox.DataBind()
  End If
End SUb

Public Sub GoButton_OnClick(sender As Object, args As EventArgs)
  Msg.Text = ""
  users = Nothing

  If RolesListBox.SelectedItem Is Nothing Then
    Msg.Text = "Please select a role."
    Return
  End If

  users = Roles.FindUsersInRole(RolesListBox.SelectedItem.Text, UsernameTextBox.Text)

  If users.Length < 1 Then
    Msg.Text = "No matching users found in selected role."
  End If

  UserGrid.DataSource = users
  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>

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

  <table border="0" cellpadding="3" cellspacing="3">
    <tr>
      <td valign="top">Role:</td>
      <td valign="top"><asp:ListBox id="RolesListBox" runat="Server" /></td>
    </tr>
    <tr>
      <td valign="top">Username to Search for:</td>
      <td valign="top"><asp:TextBox id="UsernameTextBox" runat="server" /></td>
    </tr>
  </table>
  <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>

注解

FindUsersInRole方法由 Roles 类调用,并返回角色中的用户列表,其中用户名包含为配置的 applicationName提供的 usernameToMatch 匹配项。 使用 SqlRoleProvider LIKE 关键字搜索与参数值匹配usernameToMatch的用户名,并支持SQL Server通配符。 例如,如果 usernameToMatch 参数设置为“user1”,则返回用户名为“user1”的用户的成员身份信息(如果存在)。 usernameToMatch如果参数设置为“user%”,则返回用户名为“user1”、“user2”、“user_admin”等的用户的成员身份信息。

适用于

另请参阅