Roles.GetRolesForUser Metoda

Definicja

Pobiera listę ról, w których znajduje się użytkownik.

Przeciążenia

GetRolesForUser()

Pobiera listę ról, w których znajduje się aktualnie zalogowany użytkownik.

GetRolesForUser(String)

Pobiera listę ról, w których znajduje się użytkownik.

GetRolesForUser()

Pobiera listę ról, w których znajduje się aktualnie zalogowany użytkownik.

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

Zwraca

String[]

Tablica ciągów zawierająca nazwy wszystkich ról, w których znajduje się aktualnie zalogowany użytkownik.

Wyjątki

Nie ma bieżącego zalogowanego użytkownika.

Zarządzanie rolami nie jest włączone.

Przykłady

Poniższy przykład kodu używa GetRolesForUser metody , aby pobrać listę ról dla określonego użytkownika i powiązać zwrócone role z kontrolką System.Web.UI.WebControls.GridView . Przykład pliku Web.config, który umożliwia zarządzanie rolami, zobacz 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>

Uwagi

Metoda GetRolesForUser wywołuje metodę RoleProvider.GetRolesForUser domyślnego dostawcy roli w celu pobrania ze źródła danych ról, w których znajduje się aktualnie zalogowany użytkownik. Aktualnie zalogowany użytkownik jest identyfikowany przez HttpContext.User właściwość bieżącego System.Web.HttpContextobiektu lub dla Thread.CurrentPrincipal środowisk hostingu innych niż HTTP. Jeśli żaden użytkownik nie jest zalogowany, zostanie zgłoszony wyjątek. Pobierane są tylko role aplikacji określonej we ApplicationName właściwości.

Jeśli CacheRolesInCookie jest to true, wyniki GetRolesForUser metody mogą być zwracane z pamięci podręcznej roli, a nie z określonego dostawcy roli.

Zobacz też

Dotyczy

GetRolesForUser(String)

Pobiera listę ról, w których znajduje się użytkownik.

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

Parametry

username
String

Użytkownik zwraca listę ról.

Zwraca

String[]

Tablica ciągów zawierająca nazwy wszystkich ról, w których znajduje się określony użytkownik.

Wyjątki

username to null.

username zawiera przecinek (,).

Zarządzanie rolami nie jest włączone.

Przykłady

Poniższy przykład kodu używa GetRolesForUser metody , aby pobrać listę ról dla określonego użytkownika i powiązać zwrócone role z kontrolką System.Web.UI.WebControls.GridView . Przykład pliku Web.config, który umożliwia zarządzanie rolami, zobacz 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>

Uwagi

Metoda GetRolesForUser wywołuje metodę RoleProvider.GetRolesForUser domyślnego dostawcy ról, aby pobrać ze źródła danych role, w których znajduje się użytkownik. Pobierane są tylko role aplikacji określonej we ApplicationName właściwości.

Jeśli username wartość jest równa bieżącemu zalogowanemu użytkownikowi i CacheRolesInCookie ma truewartość , wyniki GetRolesForUser metody mogą być zwracane z pamięci podręcznej roli, a nie z określonej Providerwartości .

Zobacz też

Dotyczy