WindowsTokenRoleProvider.IsUserInRole 메서드

정의

지정된 사용자가 지정된 기본 제공 Windows 역할에 있는지 여부를 나타내는 값을 가져옵니다.

오버로드

IsUserInRole(String, WindowsBuiltInRole)

지정된 사용자가 지정된 기본 제공 Windows 역할에 있는지 여부를 나타내는 값을 가져옵니다.

IsUserInRole(String, String)

지정된 사용자가 지정된 Windows 그룹에 있는지 여부를 나타내는 값을 가져옵니다.

IsUserInRole(String, WindowsBuiltInRole)

지정된 사용자가 지정된 기본 제공 Windows 역할에 있는지 여부를 나타내는 값을 가져옵니다.

public:
 bool IsUserInRole(System::String ^ username, System::Security::Principal::WindowsBuiltInRole role);
public bool IsUserInRole (string username, System.Security.Principal.WindowsBuiltInRole role);
override this.IsUserInRole : string * System.Security.Principal.WindowsBuiltInRole -> bool
Public Function IsUserInRole (username As String, role As WindowsBuiltInRole) As Boolean

매개 변수

username
String

검색할 DOMAIN\username 형태의 사용자 이름입니다.

role
WindowsBuiltInRole

검색할 Windows 역할입니다.

반환

Boolean

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

예외

username이(가) null인 경우

현재 실행 중인 사용자가 WindowsIdentity에 연결된 인증된 User를 갖고 있지 않은 경우. HTTP 이외의 시나리오에서는 현재 실행 중인 사용자가 WindowsIdentity에 연결된 인증된 CurrentPrincipal를 갖고 있지 않습니다.

또는

username이 현재 NameWindowsIdentity과 일치하지 않는 경우

예제

다음 코드 예제에서는 프로그래밍 방식으로 애플리케이션에 대 한 역할 정보를 보려는 사용자를 허용 하기 전에 현재 로그온 한 사용자가 관리자 역할에 있는지 여부를 확인 합니다. 역할 관리를 사용 하도록 설정 하는 Web.config 파일의 예제를 참조 하세요. WindowsTokenRoleProvider합니다.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<!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()
{
  Msg.Text = "";

  WindowsPrincipal p = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;

  if (!p.IsInRole(WindowsBuiltInRole.Administrator))
  {
    Msg.Text = "You are not authorized to view user roles.";
    return;
  }


  // Bind roles to GridView.

  try
  {
    rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }

  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</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()
  Msg.Text = ""

  Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)

  If Not provider.IsUserInRole(User.Identity.Name, _
                               System.Security.Principal.WindowsBuiltInRole.Administrator) Then
    Msg.Text = "You are not authorized to view user roles."
    Return
  End If


  ' Bind roles to GridView.

  Try
    rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try

  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
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>

설명

합니다 IsUserInRole 메서드를 사용 하면 사용자가에서 설명 하는 일반적인 Windows 역할 중 하나에 있는지 여부를 확인 하는 WindowsBuiltInRole 열거형입니다. 이 메서드는 여러 언어로 지역화 된 애플리케이션에 유용 합니다. 이 오버 로드는 IsUserInRole 메서드가 아닌 부분를 RoleProvider 기본 클래스 및 캐스팅 하 여 액세스할 수 있습니다를 Provider 의 속성을 Roles 클래스를 WindowsTokenRoleProvider 형식.

호출할 수 있습니다는 IsUserInRole 메서드는 현재 로그인 한 사용자에 대해서만 LOGON_USER 서버 변수에서 식별 된 합니다. 제공 된 값을 username 매개 변수는 현재 로그인 한 사용자의 이름이 아닙니다.는 HttpException throw 됩니다.

IsUserInRole 메서드는 LOGON_USER 서버 변수에서 식별 한 현재 로그인 한 사용자만 호출할 수 있습니다. 현재 로그온 한 사용자의 Windows 인증 된 사용자 여야 합니다. ASP.NET 및 Windows 인증에 대 한 자세한 내용은 참조 하세요. ASP.NET 인증합니다.

추가 정보

적용 대상

IsUserInRole(String, String)

지정된 사용자가 지정된 Windows 그룹에 있는지 여부를 나타내는 값을 가져옵니다.

public:
 override bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public override bool IsUserInRole (string username, string roleName);
override this.IsUserInRole : string * string -> bool
Public Overrides Function IsUserInRole (username As String, roleName As String) As Boolean

매개 변수

username
String

검색할 DOMAIN\username 형태의 사용자 이름입니다.

roleName
String

검색할 DOMAIN\rolename 형태의 Windows 그룹입니다.

반환

Boolean

지정된 사용자가 지정된 Windows 그룹에 있으면 true이고, 그렇지 않으면 false입니다.

예외

username이(가) null인 경우

또는

roleName이(가) null인 경우

현재 실행 중인 사용자가 WindowsIdentity에 연결된 인증된 User를 갖고 있지 않은 경우. HTTP 이외의 시나리오에서는 현재 실행 중인 사용자가 WindowsIdentity에 연결된 인증된 CurrentPrincipal를 갖고 있지 않습니다.

또는

username이 현재 NameWindowsIdentity과 일치하지 않는 경우

또는

사용자의 Windows 그룹 정보를 검색하는 동안 실패가 발생한 경우

예제

다음 코드 예제에서는 프로그래밍 방식으로 애플리케이션에 대 한 역할 정보를 보려는 사용자를 허용 하기 전에 현재 로그온 한 사용자가 관리자 역할에 있는지 여부를 확인 합니다. 역할 관리를 사용 하도록 설정 하는 Web.config 파일의 예제를 참조 하세요. WindowsTokenRoleProvider합니다.

<%@ 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()
{
  Msg.Text = "";

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


  // Bind roles to GridView.

  rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</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()
  Msg.Text = ""

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

  ' Bind roles to GridView.

  rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
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>

설명

IsUserInRole 메서드를 호출 합니다 Roles 클래스 및 IsInRole 메서드의 User 사용자 Windows 그룹 인지 여부를 결정 하는 속성입니다. 호출할 수 있습니다는 IsUserInRole 메서드는 현재 로그인 한 사용자에 대해서만 LOGON_USER 서버 변수에서 식별 된 합니다. 현재 로그온 한 사용자는 Windows 인증 된 사용자 여야 합니다. ASP.NET 및 Windows 인증에 대 한 자세한 내용은 참조 하세요. ASP.NET 인증합니다.

추가 정보

적용 대상