SqlRoleProvider.GetRolesForUser メソッド
アセンブリ: System.Web (system.web.dll 内)

Dim instance As SqlRoleProvider Dim username As String Dim returnValue As String() returnValue = instance.GetRolesForUser(username)
戻り値
指定ユーザーに割り当てられたすべてのロールの名前を格納している文字列配列。


GetRolesForUser メソッドは Roles クラスによって呼び出され、ASP.NET アプリケーションの構成ファイル (Web.config) に指定されている SQL Server データベースから、指定したユーザーのロールの一覧を取得します。

GetRolesForUser メソッドを使用して、指定されたユーザーのロールの一覧を取得し、System.Web.UI.WebControls.GridView コントロールにバインドするコード例を次に示します。ロール管理を有効にする Web.config ファイルの例については、SqlRoleProvider のトピックを参照してください。
<%@ 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 = "" 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> <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 = ""; 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> <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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からSqlRoleProvider.GetRolesForUser メソッドを検索する場合は、下記のリンクをクリックしてください。

- SqlRoleProvider.GetRolesForUser メソッドのページへのリンク