WindowsTokenRoleProvider.GetRolesForUser(String) 메서드

정의

사용자가 있는 Windows 그룹의 목록을 가져옵니다.

public:
 override cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public override string[] GetRolesForUser(string username);
override this.GetRolesForUser : string -> string[]
Public Overrides Function GetRolesForUser (username As String) As String()

매개 변수

username
String

DOMAIN\username 형식의 Windows 그룹 목록을 반환할 사용자입니다.

반품

String[]

지정된 사용자가 있는 모든 Windows 그룹의 이름을 포함하는 문자열 배열입니다.

예외

현재 실행 중인 사용자에게는 인증된 WindowsIdentity 사용자가 연결되어 User있지 않습니다. HTTP가 아닌 시나리오의 경우 현재 실행 중인 사용자에게는 인증된 WindowsIdentity 사용자가 연결되어 CurrentPrincipal있지 않습니다.

-또는-

username가 현재Name와 일치하지 WindowsIdentity 않습니다.

-또는-

사용자의 Windows 그룹 정보를 검색하는 동안 오류가 발생했습니다.

usernamenull입니다.

신뢰 수준이 .보다 Low작습니다.

예제

다음 코드 예제에서는 메서드를 GetRolesForUser 사용하여 지정된 사용자에 대한 역할 목록을 검색하고 역할 목록을 컨트롤에 GridView 바인딩합니다. 역할 관리를 사용하도록 설정하는 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>

설명

이 메서드는 Roles 클래스에서 Windows 운영 체제에서 지정된 사용자가 있는 Windows 그룹 목록을 검색하기 위해 호출됩니다. LOGON_USER GetRolesForUser 서버 변수로 식별되는 현재 로그온한 사용자에 대해서만 메서드를 호출할 수 있습니다. 매개 변수에 username 제공된 값이 현재 로그온한 사용자의 이름이 아니면 throw System.Configuration.Provider.ProviderException 됩니다.

ASP.NET 및 Windows 인증 대한 자세한 내용은 ASP.NET 인증 참조하세요.

적용 대상

추가 정보