Roles.IsUserInRole 메서드

정의

사용자가 지정된 역할에 속하는지 여부를 나타내는 값을 가져옵니다.

오버로드

IsUserInRole(String)

현재 로그온한 사용자가 지정된 역할에 속하는지 여부를 나타내는 값을 가져옵니다. API는 ASP.NET 요청 스레드의 컨텍스트 내에서만 호출되도록 고안된 것으로, 해당 권한이 있는 사용 사례에서는 스레드로부터 안전이 보장됩니다.

IsUserInRole(String, String)

지정된 사용자가 지정된 역할에 속하는지 여부를 나타내는 값을 가져옵니다. API는 ASP.NET 요청 스레드의 컨텍스트 내에서만 호출되도록 고안된 것으로, 해당 권한이 있는 사용 사례에서는 스레드로부터 안전이 보장됩니다.

IsUserInRole(String)

현재 로그온한 사용자가 지정된 역할에 속하는지 여부를 나타내는 값을 가져옵니다. API는 ASP.NET 요청 스레드의 컨텍스트 내에서만 호출되도록 고안된 것으로, 해당 권한이 있는 사용 사례에서는 스레드로부터 안전이 보장됩니다.

public:
 static bool IsUserInRole(System::String ^ roleName);
public static bool IsUserInRole (string roleName);
static member IsUserInRole : string -> bool
Public Shared Function IsUserInRole (roleName As String) As Boolean

매개 변수

roleName
String

검색할 역할의 이름입니다.

반환

Boolean

현재 로그온한 사용자가 지정된 역할에 속하면 true이고, 그렇지 않으면 false입니다.

예외

roleName이(가) null인 경우

또는

현재 로그온한 사용자가 없는 경우

roleName이 빈 문자열이거나 쉼표(,)를 포함하는 경우

역할 관리가 사용되지 않는 경우

예제

다음 코드 예제에서는 프로그래밍 방식으로 애플리케이션의 역할 설정을 볼 수 있도록 허용 하기 전에 현재 로그온 한 사용자가 관리자 역할에 있는지 여부를 확인 합니다. 역할 관리를 사용 하도록 설정 하는 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;
MembershipUserCollection users;

public void Page_Load()
{
  Msg.Text = "";

  try
  {
    if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
    {
      Msg.Text = "You are not authorized to view user roles.";
      UsersListBox.Visible = false;
      return;
    }
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }


  if (!IsPostBack)
  {
    // Bind users to ListBox.

    users = Membership.GetAllUsers();
    UsersListBox.DataSource = users;
    UsersListBox.DataBind();
  }


  // If a user is selected, show the roles for the selected user.

  if (UsersListBox.SelectedItem != null)
  {
    // Bind roles to GridView.

    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();

    UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value;
  }
}

</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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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
Dim users As MembershipUserCollection

Public Sub Page_Load()

  Msg.Text = ""

  Try
    If Not Roles.IsUserInRole(User.Identity.Name, "Administrators") Then
      Msg.Text = "You are not authorized to view user roles."
      UsersListBox.Visible = False
      Return
    End If
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try


  If Not IsPostBack Then
    ' Bind users to ListBox.

    users = Membership.GetAllUsers()
    UsersListBox.DataSource = users
    UsersListBox.DataBind()
  End If


  ' If a user is selected, show the roles for the selected user.

  If Not UsersListBox.SelectedItem Is Nothing Then
    ' Bind roles to GridView.

    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value)
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()

    UserRolesGrid.Columns(0).HeaderText = "Roles for " & UsersListBox.SelectedItem.Value
  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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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>

설명

IsUserInRole 메서드 호출을 RoleProvider.IsUserInRole 현재 로그온 한 사용자 지정 된 애플리케이션에 대 한 데이터 원본에서 역할을 사용 하 여 연결 되는지 여부를 결정 하는 기본 역할 공급자의 메서드는 ApplicationName 속성입니다. 현재 로그온한 사용자는 현재 System.Web.HttpContext속성 또는 HTTP가 아닌 호스팅 환경에 의해 Thread.CurrentPrincipal 식별 HttpContext.User 됩니다. 로그온한 사용자가 없으면 예외가 throw됩니다. 에 지정 된 애플리케이션에 대 한 역할에 대해서만 ApplicationName 속성 검색 됩니다.

true``roleName 경우 CacheRolesInCookie 지정된 역할 공급자가 아닌 역할 캐시에 대해 검사할 수 있습니다.

추가 정보

적용 대상

IsUserInRole(String, String)

지정된 사용자가 지정된 역할에 속하는지 여부를 나타내는 값을 가져옵니다. API는 ASP.NET 요청 스레드의 컨텍스트 내에서만 호출되도록 고안된 것으로, 해당 권한이 있는 사용 사례에서는 스레드로부터 안전이 보장됩니다.

public:
 static bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public static bool IsUserInRole (string username, string roleName);
static member IsUserInRole : string * string -> bool
Public Shared Function IsUserInRole (username As String, roleName As String) As Boolean

매개 변수

username
String

검색할 사용자의 이름입니다.

roleName
String

검색할 역할의 이름입니다.

반환

Boolean

지정된 사용자가 지정된 역할에 속하면 true이고, 그렇지 않으면 false입니다.

예외

roleName이(가) null인 경우

또는

username이(가) null인 경우

roleName이 빈 문자열이거나 쉼표(,)를 포함하는 경우

또는

username에 쉼표(,)가 포함된 경우

역할 관리가 사용되지 않는 경우

예제

다음 코드 예제에서는 프로그래밍 방식으로 애플리케이션의 역할 설정을 볼 수 있도록 허용 하기 전에 사용자가 관리자 역할에 있는지 여부를 확인 합니다. 역할 관리를 사용 하도록 설정 하는 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;
MembershipUserCollection users;

public void Page_Load()
{
  Msg.Text = "";

  try
  {
    if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
    {
      Msg.Text = "You are not authorized to view user roles.";
      UsersListBox.Visible = false;
      return;
    }
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }


  if (!IsPostBack)
  {
    // Bind users to ListBox.

    users = Membership.GetAllUsers();
    UsersListBox.DataSource = users;
    UsersListBox.DataBind();
  }


  // If a user is selected, show the roles for the selected user.

  if (UsersListBox.SelectedItem != null)
  {
    // Bind roles to GridView.

    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();

    UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value;
  }
}

</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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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
Dim users As MembershipUserCollection

Public Sub Page_Load()

  Msg.Text = ""

  Try
    If Not Roles.IsUserInRole(User.Identity.Name, "Administrators") Then
      Msg.Text = "You are not authorized to view user roles."
      UsersListBox.Visible = False
      Return
    End If
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try


  If Not IsPostBack Then
    ' Bind users to ListBox.

    users = Membership.GetAllUsers()
    UsersListBox.DataSource = users
    UsersListBox.DataBind()
  End If


  ' If a user is selected, show the roles for the selected user.

  If Not UsersListBox.SelectedItem Is Nothing Then
    ' Bind roles to GridView.

    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value)
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()

    UserRolesGrid.Columns(0).HeaderText = "Roles for " & UsersListBox.SelectedItem.Value
  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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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>

설명

IsUserInRole 메서드 호출을 IsUserInRole 사용자 이름에 지정 된 애플리케이션에 대 한 데이터 원본에서 역할을 사용 하 여 연결 되는지 여부를 결정 하는 기본 역할 공급자의 메서드는 ApplicationName 속성입니다.

username 현재 로그온한 사용자와 같고 CacheRolesInCookie 속성 값이면 true``roleName 지정된 Provider사용자 대신 역할 캐시에 대해 확인할 수 있습니다.

추가 정보

적용 대상