Share via


Roles.GetRolesForUser 方法

定義

取得使用者所屬角色的清單。

多載

GetRolesForUser()

取得目前登入的使用者所屬的角色清單。

GetRolesForUser(String)

取得使用者所屬角色的清單。

GetRolesForUser()

取得目前登入的使用者所屬的角色清單。

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

傳回

String[]

字串陣列,包含目前登入的使用者所屬的所有角色名稱。

例外狀況

沒有目前登入的使用者。

未啟用角色管理。

範例

下列程式碼範例會 GetRolesForUser 使用 方法來擷取指定使用者的角色清單,並將傳回的角色系結至 System.Web.UI.WebControls.GridView 控制項。 如需啟用角色管理的Web.config檔案範例,請參閱 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>

備註

方法 GetRolesForUserRoleProvider.GetRolesForUser 呼叫預設角色提供者的 方法,以從資料來源擷取目前登入使用者所在的角色。 目前登入的使用者是由 HttpContext.User 目前 System.Web.HttpContext 的 屬性所識別,或是由 Thread.CurrentPrincipal 非 HTTP 裝載環境的 屬性來識別。 如果未登入任何使用者,將會擲回例外狀況。 只會擷取屬性中指定的 ApplicationName 應用程式角色。

如果 CacheRolesInCookietrue ,則方法的結果 GetRolesForUser 可能會從角色快取傳回,而不是指定的角色提供者。

另請參閱

適用於

GetRolesForUser(String)

取得使用者所屬角色的清單。

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()

參數

username
String

要傳回角色清單的使用者。

傳回

String[]

字串陣列,包含指定使用者所屬的所有角色名稱。

例外狀況

usernamenull

username 包含逗號 (,)。

未啟用角色管理。

範例

下列程式碼範例會 GetRolesForUser 使用 方法來擷取指定使用者的角色清單,並將傳回的角色系結至 System.Web.UI.WebControls.GridView 控制項。 如需啟用角色管理的Web.config檔案範例,請參閱 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>

備註

方法 GetRolesForUserRoleProvider.GetRolesForUser 呼叫預設角色提供者的 方法來擷取使用者所在的角色資料來源。 只會擷取屬性中指定的 ApplicationName 應用程式角色。

如果 username 等於目前的登入使用者,且 CacheRolesInCookietrue ,則方法的結果 GetRolesForUser 可能會從角色快取傳回,而不是指定的 Provider

另請參閱

適用於