Udostępnij za pośrednictwem


RolePrincipal.GetRoles Metoda

Definicja

Pobiera listę ról, których RolePrincipal element jest członkiem.

public:
 cli::array <System::String ^> ^ GetRoles();
public string[] GetRoles ();
member this.GetRoles : unit -> string[]
Public Function GetRoles () As String()

Zwraca

String[]

Lista ról, których RolePrincipal element jest członkiem.

Wyjątki

Właściwość Identity to null.

Przykłady

Poniższy przykład kodu przedstawia role, których zalogowany użytkownik jest członkiem.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

string[] rolesArray;

public void Page_Load()
{
  RolePrincipal r = (RolePrincipal)User;
  rolesArray = r.GetRoles();
  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  Heading.Text = "Roles for " + User.Identity.Name;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample: View Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3><asp:Label id="Heading" runat="server" /></h3>

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <Columns>
                           <asp:TemplateField >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

Dim rolesArray() As String

Public Sub Page_Load()
  Dim r As RolePrincipal = CType(User, RolePrincipal)
  rolesArray = r.GetRoles()
  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  Heading.Text = "Roles for " & User.Identity.Name
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sample: View Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3><asp:Label id="Heading" runat="server" /></h3>

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <Columns>
                           <asp:TemplateField >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>

Uwagi

Jeśli CacheRolesInCookie nazwa jest i true nazwy ról zostały buforowane dla użytkownika reprezentowanego przez RolePrincipalmetodę , GetRoles metoda zwróci listę nazw ról w pamięci podręcznej, z którą użytkownik jest członkiem. Jeśli nazwy ról nie zostały buforowane, GetRoles metoda zwraca listę nazw ról, które użytkownik jest członkiem roli Providerdomyślnej . Zwracane są tylko nazwy ról dla bieżącego ApplicationName . Aby określić, czy nazwy ról zostały buforowane w RolePrincipal obiekcie, sprawdź IsRoleListCached właściwość .

Dotyczy

Zobacz też