Roles.IsUserInRole Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene un valore che indica se un utente appartiene al ruolo specificato.
Overload
IsUserInRole(String) |
Ottiene un valore che indica se l'utente attualmente connesso al sistema appartiene al ruolo specificato. L'API deve essere chiamata solo all'interno del contesto di un thread di richiesta ASP.NET e in tale caso d'uso sanzionato è thread-safe. |
IsUserInRole(String, String) |
Ottiene un valore che indica se l'utente specificato appartiene al ruolo specificato. L'API deve essere chiamata solo all'interno del contesto di un thread di richiesta ASP.NET e in tale caso d'uso sanzionato è thread-safe. |
IsUserInRole(String)
Ottiene un valore che indica se l'utente attualmente connesso al sistema appartiene al ruolo specificato. L'API deve essere chiamata solo all'interno del contesto di un thread di richiesta ASP.NET e in tale caso d'uso sanzionato è thread-safe.
public:
static bool IsUserInRole(System::String ^ roleName);
public static bool IsUserInRole (string roleName);
static member IsUserInRole : string -> bool
Public Shared Function IsUserInRole (roleName As String) As Boolean
Parametri
- roleName
- String
Nome del ruolo in cui eseguire la ricerca.
Restituisce
true
se l'utente correntemente connesso appartiene al ruolo specificato; in caso contrario, false
.
Eccezioni
roleName
è una stringa vuota o contiene una virgola (,).
La gestione ruoli non è attivata.
Esempio
Nell'esempio di codice seguente viene verificato a livello di codice se l'utente attualmente connesso è nel ruolo Administrators prima di consentire all'utente di visualizzare le impostazioni dei ruoli per l'applicazione. Per un esempio di file di Web.config che abilita la gestione dei ruoli, vedere Roles.
<%@ 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[] rolesArray;
MembershipUserCollection users;
public void Page_Load()
{
Msg.Text = "";
try
{
if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
{
Msg.Text = "You are not authorized to view user roles.";
UsersListBox.Visible = false;
return;
}
}
catch (HttpException e)
{
Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
return;
}
if (!IsPostBack)
{
// Bind users to ListBox.
users = Membership.GetAllUsers();
UsersListBox.DataSource = users;
UsersListBox.DataBind();
}
// If a user is selected, show the roles for the selected user.
if (UsersListBox.SelectedItem != null)
{
// Bind roles to GridView.
rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
UserRolesGrid.DataSource = rolesArray;
UserRolesGrid.DataBind();
UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username"
Rows="8" AutoPostBack="true" runat="server" /></td>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</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 rolesArray() As String
Dim users As MembershipUserCollection
Public Sub Page_Load()
Msg.Text = ""
Try
If Not Roles.IsUserInRole(User.Identity.Name, "Administrators") Then
Msg.Text = "You are not authorized to view user roles."
UsersListBox.Visible = False
Return
End If
Catch e As HttpException
Msg.Text = "There is no current logged on user. Role membership cannot be verified."
Return
End Try
If Not IsPostBack Then
' Bind users to ListBox.
users = Membership.GetAllUsers()
UsersListBox.DataSource = users
UsersListBox.DataBind()
End If
' If a user is selected, show the roles for the selected user.
If Not UsersListBox.SelectedItem Is Nothing Then
' Bind roles to GridView.
rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value)
UserRolesGrid.DataSource = rolesArray
UserRolesGrid.DataBind()
UserRolesGrid.Columns(0).HeaderText = "Roles for " & UsersListBox.SelectedItem.Value
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username"
Rows="8" AutoPostBack="true" runat="server" /></td>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</form>
</body>
</html>
Commenti
Il IsUserInRole metodo chiama il RoleProvider.IsUserInRole metodo del provider di ruoli predefinito per determinare se l'utente attualmente connesso è associato a un ruolo dall'origine dati per l'applicazione specificata nella ApplicationName proprietà . L'utente attualmente connesso è identificato dalla HttpContext.User proprietà dell'oggetto corrente System.Web.HttpContexto da Thread.CurrentPrincipal per gli ambienti di hosting non HTTP. Se nessun utente è connesso, verrà generata un'eccezione. Vengono recuperati solo i ruoli per l'applicazione specificata nella ApplicationName proprietà .
Se CacheRolesInCookie è true
, roleName
è possibile verificare la cache dei ruoli anziché il provider di ruoli specificato.
Vedi anche
Si applica a
IsUserInRole(String, String)
Ottiene un valore che indica se l'utente specificato appartiene al ruolo specificato. L'API deve essere chiamata solo all'interno del contesto di un thread di richiesta ASP.NET e in tale caso d'uso sanzionato è thread-safe.
public:
static bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public static bool IsUserInRole (string username, string roleName);
static member IsUserInRole : string * string -> bool
Public Shared Function IsUserInRole (username As String, roleName As String) As Boolean
Parametri
- username
- String
Nome dell'utente da cercare.
- roleName
- String
Nome del ruolo in cui eseguire la ricerca.
Restituisce
true
se l'utente specificato appartiene al ruolo specificato; in caso contrario, false
.
Eccezioni
roleName
è una stringa vuota o contiene una virgola (,).
-oppure-
username
contiene una virgola (,).
La gestione ruoli non è attivata.
Esempio
Nell'esempio di codice seguente viene verificato a livello di codice se un utente è nel ruolo Administrators prima di consentire all'utente di visualizzare le impostazioni dei ruoli per l'applicazione. Per un esempio di file di Web.config che abilita la gestione dei ruoli, vedere Roles.
<%@ 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[] rolesArray;
MembershipUserCollection users;
public void Page_Load()
{
Msg.Text = "";
try
{
if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
{
Msg.Text = "You are not authorized to view user roles.";
UsersListBox.Visible = false;
return;
}
}
catch (HttpException e)
{
Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
return;
}
if (!IsPostBack)
{
// Bind users to ListBox.
users = Membership.GetAllUsers();
UsersListBox.DataSource = users;
UsersListBox.DataBind();
}
// If a user is selected, show the roles for the selected user.
if (UsersListBox.SelectedItem != null)
{
// Bind roles to GridView.
rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
UserRolesGrid.DataSource = rolesArray;
UserRolesGrid.DataBind();
UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username"
Rows="8" AutoPostBack="true" runat="server" /></td>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</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 rolesArray() As String
Dim users As MembershipUserCollection
Public Sub Page_Load()
Msg.Text = ""
Try
If Not Roles.IsUserInRole(User.Identity.Name, "Administrators") Then
Msg.Text = "You are not authorized to view user roles."
UsersListBox.Visible = False
Return
End If
Catch e As HttpException
Msg.Text = "There is no current logged on user. Role membership cannot be verified."
Return
End Try
If Not IsPostBack Then
' Bind users to ListBox.
users = Membership.GetAllUsers()
UsersListBox.DataSource = users
UsersListBox.DataBind()
End If
' If a user is selected, show the roles for the selected user.
If Not UsersListBox.SelectedItem Is Nothing Then
' Bind roles to GridView.
rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value)
UserRolesGrid.DataSource = rolesArray
UserRolesGrid.DataBind()
UserRolesGrid.Columns(0).HeaderText = "Roles for " & UsersListBox.SelectedItem.Value
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username"
Rows="8" AutoPostBack="true" runat="server" /></td>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</form>
</body>
</html>
Commenti
Il IsUserInRole metodo chiama il IsUserInRole metodo del provider di ruoli predefinito per determinare se un nome utente è associato a un ruolo dall'origine dati per l'applicazione specificata nella ApplicationName proprietà .
Se username
è uguale all'utente connesso corrente e il valore della CacheRolesInCookie proprietà è true
, roleName
è possibile verificare la cache dei ruoli anziché l'oggetto specificato Provider.
Vedi anche
Si applica a
Commenti e suggerimenti
https://aka.ms/ContentUserFeedback.
Presto disponibile: Nel corso del 2024 verranno gradualmente disattivati i problemi di GitHub come meccanismo di feedback per il contenuto e ciò verrà sostituito con un nuovo sistema di feedback. Per altre informazioni, vedereInvia e visualizza il feedback per