RolePrincipal.GetRoles メソッド

定義

RolePrincipal がメンバーになっているロールの一覧を取得します。

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

戻り値

String[]

RolePrincipal がメンバーになっているロールの一覧。

例外

Identity プロパティが null です。

次のコード例では、ログオン ユーザーがメンバーになっているロールを表示します。

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

注釈

によって表されるユーザーに対して is true と ロール名がキャッシュされているGetRolesRolePrincipal場合CacheRolesInCookie、メソッドは、ユーザーがメンバーであるキャッシュ内のロール名の一覧を返します。 ロール名がキャッシュされていない場合、 メソッドは、 GetRoles ユーザーがメンバーであるロール名の一覧を既定のロール Providerから返します。 現在 ApplicationName のロール名のみが返されます。 ロール名がオブジェクトに RolePrincipal キャッシュされているかどうかを確認するには、 プロパティを IsRoleListCached 確認します。

適用対象

こちらもご覧ください