Partage via


WindowsTokenRoleProvider.IsUserInRole Méthode

Définition

Obtient une valeur indiquant si l’utilisateur spécifié se trouve dans le rôle Windows intégré spécifié.

Surcharges

Nom Description
IsUserInRole(String, WindowsBuiltInRole)

Obtient une valeur indiquant si l’utilisateur spécifié se trouve dans le rôle Windows intégré spécifié.

IsUserInRole(String, String)

Obtient une valeur indiquant si l’utilisateur spécifié se trouve dans le groupe Windows spécifié.

IsUserInRole(String, WindowsBuiltInRole)

Obtient une valeur indiquant si l’utilisateur spécifié se trouve dans le rôle Windows intégré spécifié.

public:
 bool IsUserInRole(System::String ^ username, System::Security::Principal::WindowsBuiltInRole role);
public bool IsUserInRole(string username, System.Security.Principal.WindowsBuiltInRole role);
override this.IsUserInRole : string * System.Security.Principal.WindowsBuiltInRole -> bool
Public Function IsUserInRole (username As String, role As WindowsBuiltInRole) As Boolean

Paramètres

username
String

Nom d’utilisateur à rechercher dans le formulaire DOMAIN\username.

role
WindowsBuiltInRole

Rôle Windows dans lequel effectuer une recherche.

Retours

true si l’utilisateur spécifié se trouve dans le rôle Windows spécifié ; sinon, false.

Exceptions

username a la valeur null.

L’utilisateur en cours d’exécution n’a pas d’attachement authentifié WindowsIdentity à User. Pour les scénarios non HTTP, l’utilisateur en cours d’exécution n’a pas d’attachement authentifié WindowsIdentity à CurrentPrincipal.

- ou -

username ne correspond pas à Name la valeur actuelle WindowsIdentity.

Exemples

L’exemple de code suivant vérifie par programmation si l’utilisateur actuellement connecté se trouve dans le rôle Administrateurs avant de permettre à l’utilisateur d’afficher les informations de rôles pour l’application. Pour obtenir un exemple de fichier Web.config qui active la gestion des rôles, consultez WindowsTokenRoleProvider.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<!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;

public void Page_Load()
{
  Msg.Text = "";

  WindowsPrincipal p = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;

  if (!p.IsInRole(WindowsBuiltInRole.Administrator))
  {
    Msg.Text = "You are not authorized to view user roles.";
    return;
  }


  // Bind roles to GridView.

  try
  {
    rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }

  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</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: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

Public Sub Page_Load()
  Msg.Text = ""

  Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)

  If Not provider.IsUserInRole(User.Identity.Name, _
                               System.Security.Principal.WindowsBuiltInRole.Administrator) Then
    Msg.Text = "You are not authorized to view user roles."
    Return
  End If


  ' Bind roles to GridView.

  Try
    rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try

  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
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: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>

Remarques

La IsUserInRole méthode vous permet de vérifier si un utilisateur se trouve dans l’un des rôles Windows courants décrits par l’énumération WindowsBuiltInRole . Cette méthode est utile pour les applications localisées dans plusieurs langues. Cette surcharge de la IsUserInRole méthode ne fait pas partie de la RoleProvider classe de base et est accessible uniquement en cas de conversion de la Provider propriété de la Roles classe en tant que WindowsTokenRoleProvider type.

Vous pouvez appeler la IsUserInRole méthode uniquement pour l’utilisateur actuellement connecté, comme identifié par la variable de serveur LOGON_USER. Si la valeur fournie dans le username paramètre n’est pas le nom de l’utilisateur actuellement connecté, une HttpException valeur est levée.

IsUserInRole la méthode ne peut être appelée que pour l’utilisateur actuellement connecté identifié par la variable de serveur LOGON_USER. L’utilisateur connecté actuel doit être un utilisateur authentifié Windows. Pour plus d’informations sur ASP.NET et l’authentification Windows, consultez ASP.NET Authentification.

Voir aussi

S’applique à

IsUserInRole(String, String)

Obtient une valeur indiquant si l’utilisateur spécifié se trouve dans le groupe Windows spécifié.

public:
 override bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public override bool IsUserInRole(string username, string roleName);
override this.IsUserInRole : string * string -> bool
Public Overrides Function IsUserInRole (username As String, roleName As String) As Boolean

Paramètres

username
String

Nom d’utilisateur à rechercher dans le formulaire DOMAIN\username.

roleName
String

Groupe Windows à rechercher dans le formulaire DOMAIN\rolename.

Retours

true si le nom d’utilisateur spécifié se trouve dans le groupe Windows spécifié ; sinon, false.

Exceptions

username a la valeur null.

- ou -

roleName a la valeur null.

L’utilisateur en cours d’exécution n’a pas d’attachement authentifié WindowsIdentity à User. Pour les scénarios non HTTP, l’utilisateur en cours d’exécution n’a pas d’attachement authentifié WindowsIdentity à CurrentPrincipal.

- ou -

username ne correspond pas à Name la valeur actuelle WindowsIdentity.

- ou -

Une défaillance s’est produite lors de la récupération des informations de groupe Windows de l’utilisateur.

Exemples

L’exemple de code suivant vérifie par programmation si l’utilisateur actuellement connecté se trouve dans le rôle Administrateurs avant de permettre à l’utilisateur d’afficher les informations de rôles pour l’application. Pour obtenir un exemple de fichier Web.config qui active la gestion des rôles, consultez WindowsTokenRoleProvider.

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

public void Page_Load()
{
  Msg.Text = "";

  try
  {
    if (!Roles.IsUserInRole(User.Identity.Name, @"BUILTIN\Administrators"))
    {
      Msg.Text = "You are not authorized to view user roles.";
      return;
    }
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }


  // Bind roles to GridView.

  rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</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: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

Public Sub Page_Load()
  Msg.Text = ""

  Try
    If Not Roles.IsUserInRole(User.Identity.Name, "BUILTIN\Administrators") Then
      Msg.Text = "You are not authorized to view user roles."
      Return
    End If
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try

  ' Bind roles to GridView.

  rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
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: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>

Remarques

La IsUserInRole méthode est appelée par la Roles classe et la IsInRole méthode de la User propriété pour déterminer si un utilisateur se trouve dans un groupe Windows. Vous pouvez appeler la IsUserInRole méthode uniquement pour l’utilisateur actuellement connecté, comme identifié par la variable de serveur LOGON_USER. L’utilisateur connecté actuel doit être un utilisateur authentifié Windows. Pour plus d’informations sur ASP.NET et l’authentification Windows, consultez ASP.NET Authentification.

Voir aussi

S’applique à