RolePrincipal.IsInRole メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > RolePrincipal.IsInRole メソッドの意味・解説 

RolePrincipal.IsInRole メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

RolePrincipal が示すユーザーが、指定したロールに存在するかどうかを示す値を取得します。

名前空間: System.Web.Security
アセンブリ: System.Web (system.web.dll 内)
構文構文

解説解説

IsInRole はまず IsRoleListCached をチェックして、現在のユーザーのロール名のキャッシュされた一覧があるかどうかを確認します。IsRoleListCached プロパティが true の場合、指定された role に対してキャッシュされた一覧があるかどうかをチェックします。ロール名が Cookie にキャッシュされる場合、ユーザーに許可されているロール名と拒否されているロール名の両方がキャッシュされます。指定された role が許可されたロール名のキャッシュされた一覧に見つかると、IsInRole メソッドは true を返します。指定された role が拒否されたロール名のキャッシュされた一覧に見つかると、IsInRole メソッドは false を返します。IsInRole が、指定された role をロール名の許可および拒否された一覧で見つけることができない場合は、既定のロール Provider の IsUserInRole メソッドを呼び出し、構成された ApplicationName のデータ ソースのロールにユーザー名が関連付けられているかどうかを確認します。

使用例使用例

ログオンしているユーザーにユーザー ロールの参照を許可する前に、ユーザーが Administrators ロールに属しているかどうかをチェックするコード例を次に示します。

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security"
 %>
<script runat="server">

Dim rolesArray() As String
Dim users As MembershipUserCollection

Public Sub Page_Load()

  Msg.Text = ""

  If Not User.IsInRole("Administrators")
 Then
    Msg.Text = "You are not authorized to view user roles."
    UsersListBox.Visible = False
    Return
  End If

  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>
<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"
 runat="server" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

string[] rolesArray;
MembershipUserCollection users;

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

  if (!User.IsInRole("Administrators"))
  {
    Msg.Text = "You are not authorized to view user roles.";
    UsersListBox.Visible = false;
    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>
<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" runat="server"
 >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

RolePrincipal.IsInRole メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



RolePrincipal.IsInRole メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS