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 且該使用者是指定角色的成員。

例外狀況

roleNamenullNothing 在 Visual Basic 中)。

-或-

usernameToMatchnull

roleName 是空字串或包含逗號。

-或-

usernameToMatch 是空字串。

-或-

roleName 長度超過256個字元。

-或-

usernameToMatch 長度超過256個字元。

roleName 在資料庫中找不到。

-或-

與資料庫通訊時發生了未知錯誤。

範例

以下程式碼範例使用此 FindUsersInRole 方法,根據使用者輸入顯示角色成員身份。 關於啟用角色管理的 Web.config 檔案範例,請參見 Roles

Important

此範例包含一個接受使用者輸入的文字框,這可能構成安全威脅。 預設情況下,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類別呼叫,並回傳一個角色中使用者的清單,其中使用者名稱包含與所usernameToMatch提供的 applicationName 相符。 SqlRoleProvider 會使用 LIKE 關鍵字搜尋與 usernameToMatch 參數值相符的使用者名稱,並支援SQL Server萬用字元。 例如,若 usernameToMatch 參數設為「user1」,則會回傳使用者名稱為「user1」的成員資訊(若存在)。 若 usernameToMatch 參數設為「user%」,則會回傳使用者名稱為「user1」、「user2」、「user_admin」等的成員資訊。

適用於

另請參閱