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 trueCacheRolesInCookie 和 角色名稱已針對 所 RolePrincipal 代表的使用者快取,則 GetRoles 方法會傳回使用者所屬快取中的角色名稱清單。 如果尚未快取角色名稱, GetRoles 則方法會傳回使用者從預設角色 Provider 成員的角色名稱清單。 只會傳回目前 ApplicationName 的角色名稱。 若要判斷物件中 RolePrincipal 是否已快取角色名稱,請檢查 IsRoleListCached 屬性。

適用於

另請參閱