SqlRoleProvider.FindUsersInRole(String, String) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Obtiene una matriz de nombres de usuario en un rol donde el nombre de usuario contiene el nombre de usuario especificado que debe coincidir.
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()
Parámetros
- roleName
- String
Rol en el que se va a buscar.
- usernameToMatch
- String
Nombre de usuario que se va a buscar.
Devoluciones
Matriz de cadenas que contiene los nombres de todos los usuarios donde coincide usernameToMatch el nombre de usuario y el usuario es miembro del rol especificado.
Excepciones
roleName es una cadena vacía o contiene una coma.
O bien
usernameToMatch es una cadena vacía.
O bien
roleName tiene más de 256 caracteres.
O bien
usernameToMatch tiene más de 256 caracteres.
roleName no se encontró en la base de datos.
O bien
Error desconocido al comunicarse con la base de datos.
Ejemplos
En el ejemplo de código siguiente se usa el método para mostrar la FindUsersInRole pertenencia a roles en función de la entrada del usuario. Para obtener un ejemplo de un archivo de Web.config que habilita la administración de roles, vea Roles.
Importante
Este ejemplo contiene un cuadro de texto que acepta la entrada del usuario, que es una amenaza de seguridad potencial. De forma predeterminada, ASP.NET páginas web validan que la entrada del usuario no incluye elementos HTML ni de script. Para obtener más información, consulte Información general sobre vulnerabilidades de seguridad de script.
<%@ 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>
Comentarios
La FindUsersInRole clase llama Roles al método y devuelve una lista de usuarios en un rol donde el nombre de usuario contiene una coincidencia del proporcionado usernameToMatch para el configurado applicationName. El usernameToMatch parámetro se establece en "user%", se devuelve la información de pertenencia de los usuarios con el nombre de usuario "user1", "user2", "user_admin", etc.