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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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">

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」 等的使用者成員資格資訊。

適用於

另請參閱