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>

注解

该方法 GetRolesForUser 调用 RoleProvider.GetRolesForUser 默认角色提供程序的方法,以便从数据源中检索当前登录用户所处于的角色。 当前登录的用户由HttpContext.User当前System.Web.HttpContextThread.CurrentPrincipal属性或非 HTTP 托管环境标识。 如果未登录任何用户,将引发异常。 仅检索属性中指定的 ApplicationName 应用程序的角色。

true如果是CacheRolesInCookie,则方法的结果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>

注解

该方法 GetRolesForUser 调用 RoleProvider.GetRolesForUser 默认角色提供程序的方法,以从用户在其中的角色的数据源中检索。 仅检索属性中指定的 ApplicationName 应用程序的角色。

如果 username 等于当前登录用户,并且 CacheRolesInCookietrue,则方法的结果 GetRolesForUser 可能会从角色缓存而不是指定 Provider返回。

另请参阅

适用于