RolePrincipal.GetRoles Método
Definição
Obtém uma lista de funções das quais o RolePrincipal é membro.Gets a list of roles that the RolePrincipal is a member of.
public:
cli::array <System::String ^> ^ GetRoles();
public string[] GetRoles ();
member this.GetRoles : unit -> string[]
Public Function GetRoles () As String()
Retornos
- String[]
A lista de funções das quais o RolePrincipal é membro.The list of roles that the RolePrincipal is a member of.
Exceções
Exemplos
O exemplo de código a seguir exibe as funções das quais o usuário conectado é membro.The following code example displays the roles that the logged-on user is a member of.
<%@ 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>
Comentários
Se CacheRolesInCookie for true e os nomes de função tiverem sido armazenados em cache para o usuário representado pelo RolePrincipal , o GetRoles método retornará a lista de nomes de função no cache do qual o usuário é membro.If CacheRolesInCookie is true and role names have been cached for the user represented by the RolePrincipal, then the GetRoles method will return the list of role names in the cache that the user is a member of. Se os nomes de função não tiverem sido armazenados em cache, o GetRoles método retornará a lista de nomes de função dos quais o usuário é membro da função padrão Provider .If role names have not been cached, then the GetRoles method returns the list of role names that the user is a member of from the default role Provider. Somente os nomes de função para o atual ApplicationName são retornados.Only role names for the current ApplicationName are returned. Para determinar se os nomes de função foram armazenados em cache no RolePrincipal objeto, verifique a IsRoleListCached propriedade.To determine whether role names have been cached in the RolePrincipal object, check the IsRoleListCached property.