Sdílet prostřednictvím


Roles.GetRolesForUser Metoda

Definice

Získá seznam rolí, ve které je uživatel.

Přetížení

Name Description
GetRolesForUser()

Získá seznam rolí, ve které je aktuálně přihlášený uživatel.

GetRolesForUser(String)

Získá seznam rolí, ve které je uživatel.

GetRolesForUser()

Získá seznam rolí, ve které je aktuálně přihlášený uživatel.

public:
 static cli::array <System::String ^> ^ GetRolesForUser();
public static string[] GetRolesForUser();
static member GetRolesForUser : unit -> string[]
Public Shared Function GetRolesForUser () As String()

Návraty

String[]

Pole řetězců obsahující názvy všech rolí, ve které je aktuálně přihlášený uživatel.

Výjimky

Neexistuje žádný aktuální přihlášený uživatel.

Správa rolí není povolená.

Příklady

Následující příklad kódu používá metodu GetRolesForUser k načtení seznamu rolí pro zadaného uživatele a vazbu vrácených rolí na System.Web.UI.WebControls.GridView ovládací prvek. Příklad souboru Web.config, který umožňuje správu rolí, najdete v tématu 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;

public void Page_Load()
{
  if (!IsPostBack)
  {
    // Bind roles to GridView.

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

    UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();
  }
}

</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()
  If Not IsPostBack Then
    ' Bind roles to GridView.

    Try
      rolesArray = Roles.GetRolesForUser()
    Catch e As HttpException
      Msg.Text = "There is no current logged on user. Role information cannot be retrieved."
      Return
    End Try

    UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()
  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: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 GetRolesForUser volá metodu RoleProvider.GetRolesForUser výchozího zprostředkovatele role, která načítá ze zdroje dat role, ve které je aktuálně přihlášený uživatel. Aktuálně přihlášený uživatel je identifikován HttpContext.User vlastností aktuálního System.Web.HttpContextThread.CurrentPrincipal nebo jiného hostitelského prostředí než HTTP. Pokud není přihlášený žádný uživatel, vyvolá se výjimka. Načtou se pouze role pro aplikaci, která je zadaná ve ApplicationName vlastnosti.

Pokud CacheRolesInCookie je true, výsledky GetRolesForUser metody mohou být vráceny z mezipaměti role místo určeného zprostředkovatele role.

Viz také

Platí pro

GetRolesForUser(String)

Získá seznam rolí, ve které je uživatel.

public:
 static cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public static string[] GetRolesForUser(string username);
static member GetRolesForUser : string -> string[]
Public Shared Function GetRolesForUser (username As String) As String()

Parametry

username
String

Uživatel vrátí seznam rolí.

Návraty

String[]

Pole řetězců obsahující názvy všech rolí, ve které je zadaný uživatel.

Výjimky

username je null.

username obsahuje čárku (,).

Správa rolí není povolená.

Příklady

Následující příklad kódu používá metodu GetRolesForUser k načtení seznamu rolí pro zadaného uživatele a vazbu vrácených rolí na System.Web.UI.WebControls.GridView ovládací prvek. Příklad souboru Web.config, který umožňuje správu rolí, najdete v tématu 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;

public void Page_Load()
{
  if (!IsPostBack)
  {
    // Bind roles to GridView.

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

    UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();
  }
}

</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()
  If Not IsPostBack Then
    ' Bind roles to GridView.

    Try
      rolesArray = Roles.GetRolesForUser()
    Catch e As HttpException
      Msg.Text = "There is no current logged on user. Role information cannot be retrieved."
      Return
    End Try

    UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()
  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: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 GetRolesForUser volá metodu RoleProvider.GetRolesForUser výchozího zprostředkovatele role, která načítá ze zdroje dat role, ve které je uživatel. Načtou se pouze role pro aplikaci, která je zadaná ve ApplicationName vlastnosti.

Pokud username je rovna aktuálnímu přihlášeného uživatele a CacheRolesInCookie je true, výsledky GetRolesForUser metody mohou být vráceny z mezipaměti role místo zadané Provider.

Viz také

Platí pro