Sdílet prostřednictvím


WindowsTokenRoleProvider.IsUserInRole Metoda

Definice

Získá hodnotu označující, zda je zadaný uživatel v zadané předdefinované roli systému Windows.

Přetížení

IsUserInRole(String, WindowsBuiltInRole)

Získá hodnotu označující, zda je zadaný uživatel v zadané předdefinované roli systému Windows.

IsUserInRole(String, String)

Získá hodnotu označující, zda je zadaný uživatel v zadané skupině systému Windows.

IsUserInRole(String, WindowsBuiltInRole)

Získá hodnotu označující, zda je zadaný uživatel v zadané předdefinované roli systému Windows.

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

Parametry

username
String

Uživatelské jméno, které chcete vyhledat ve formátu DOMÉNA\uživatelské_jméno.

role
WindowsBuiltInRole

Role Windows, ve které se má hledat.

Návraty

truePokud je zadaný uživatel v zadané roli systému Windows; v opačném případě . false

Výjimky

username je null.

Aktuálně spuštěný uživatel nemá ověřený WindowsIdentity připojený k User. V případě jiných scénářů než HTTP nemá aktuálně spuštěný uživatel připojený ověřený WindowsIdentity soubor .CurrentPrincipal

-nebo-

username neodpovídá Name aktuální WindowsIdentityhodnotě .

Příklady

Následující příklad kódu programově zkontroluje, jestli je aktuálně přihlášený uživatel v roli Administrators, než uživateli povolí zobrazit informace o rolích pro aplikaci. Příklad souboru Web.config, který umožňuje správu rolí, najdete v tématu 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>

Poznámky

Metoda IsUserInRole umožňuje zkontrolovat, zda je uživatel v některé z běžných rolí systému Windows popsaných ve výčtu WindowsBuiltInRole . Tato metoda je užitečná pro aplikace, které jsou lokalizovány do více jazyků. Toto přetížení IsUserInRole metody není součástí RoleProvider základní třídy a lze k němu přistupovat pouze přetypováním Provider vlastnosti Roles třídy jako WindowsTokenRoleProvider typu.

Metodu IsUserInRole můžete volat pouze pro aktuálně přihlášeného uživatele podle proměnné LOGON_USER serveru. Pokud hodnota zadaná v parametru username není jméno aktuálně přihlášeného uživatele, HttpException vyvolá se chyba .

IsUserInRole Metodu lze volat pouze pro aktuálně přihlášeného uživatele identifikovaného proměnnou LOGON_USER serveru. Aktuálně přihlášený uživatel musí být ověřeným uživatelem systému Windows. Další informace o ASP.NET a ověřování systému Windows najdete v tématu Ověřování ASP.NET.

Viz také

Platí pro

IsUserInRole(String, String)

Získá hodnotu označující, zda je zadaný uživatel v zadané skupině systému Windows.

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

Parametry

username
String

Uživatelské jméno, které chcete vyhledat ve formátu DOMÉNA\uživatelské_jméno.

roleName
String

Skupinu Windows, která se má hledat ve formátu DOMÉNA\název_role.

Návraty

truePokud zadané uživatelské jméno je v zadané skupině systému Windows; v opačném případě . false

Výjimky

username je null.

-nebo-

roleName je null.

Aktuálně spuštěný uživatel nemá ověřený WindowsIdentity připojený k User. V případě jiných scénářů než HTTP nemá aktuálně spuštěný uživatel připojený ověřený WindowsIdentity soubor .CurrentPrincipal

-nebo-

username neodpovídá Name aktuální WindowsIdentityhodnotě .

-nebo-

Při načítání informací o skupině windows uživatele došlo k chybě.

Příklady

Následující příklad kódu programově zkontroluje, jestli je aktuálně přihlášený uživatel v roli Administrators, než uživateli povolí zobrazit informace o rolích pro aplikaci. Příklad souboru Web.config, který umožňuje správu rolí, najdete v tématu 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>

Poznámky

Metoda IsUserInRole je volána Roles třídou a IsInRole metodou User vlastnosti k určení, zda je uživatel ve skupině systému Windows. Metodu IsUserInRole můžete volat pouze pro aktuálně přihlášeného uživatele podle proměnné LOGON_USER serveru. Aktuální přihlášený uživatel musí být ověřeným uživatelem systému Windows. Další informace o ASP.NET a ověřování systému Windows najdete v tématu Ověřování ASP.NET.

Viz také

Platí pro