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 하여 현재 로그온한 사용자가 있는 역할을 데이터 원본에서 검색합니다. 현재 로그온한 사용자는 현재 System.Web.HttpContext속성 또는 HTTP가 아닌 호스팅 환경에 의해 Thread.CurrentPrincipal 식별 HttpContext.User 됩니다. 로그온한 사용자가 없으면 예외가 throw됩니다. 에 지정 된 애플리케이션에 대 한 역할에 대해서만 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[]

지정된 사용자가 속한 모든 역할의 이름이 포함된 문자열 배열입니다.

예외

username이(가) null인 경우

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 CacheRolesInCookie true메서드의 GetRolesForUser 결과가 지정된 Provider것이 아니라 역할 캐시에서 반환될 수 있습니다.

추가 정보

적용 대상