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




ユーザーをロールに追加するコード例を次に示します。ロール管理を有効にする Web.config ファイルの例については、Roles のトピックを参照してください。
<%@ Page Language="VB" %> <%@ Import Namespace="System.Web.Security" %> <%@ Import Namespace="System.Web.UI" %> <script runat="server"> Dim rolesArray() As String Dim users As MembershipUserCollection Public Sub Page_Load() Msg.Text = "" If Not IsPostBack Then ' Bind roles to ListBox. rolesArray = Roles.GetAllRoles() RolesListBox.DataSource = rolesArray RolesListBox.DataBind() ' Bind users to ListBox. users = Membership.GetAllUsers() UsersListBox.DataSource = users UsersListBox.DataBind() End If End Sub Public Sub AddUser_OnClick(sender As Object, args As EventArgs) ' Verify that a user and a role are selected. If UsersListBox.SelectedItem Is Nothing Then Msg.Text = "Please select a user." Return End If If RolesListBox.SelectedItem Is Nothing Then Msg.Text = "Please select a role." Return End If ' Add the user to the selected role. Try Roles.AddUserToRole(UsersListBox.SelectedItem.Value, RolesListBox.SelectedItem.Value) Msg.Text = "User added to Role." Catch e As HttpException Msg.Text = e.Message End Try End Sub </script> <html> <head> <title>Sample: Role Membership</title> </head> <body> <form runat="server" id="PageForm"> <h3>Role Membership</h3> <asp:Label id="Msg" ForeColor="maroon" runat="server" /><BR> <table CellPadding="3" border="0"> <tr> <td valign="top">Roles:</td> <td valign="top"><asp:ListBox id="RolesListBox" runat="server" Rows="8" /></td> <td valign="top">Users:</td> <td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username" Rows="8" runat="server" /></td> <td valign="top"><asp:Button Text="Add User to Role" id="AddUserButton" runat="server" OnClick="AddUser_OnClick" /></td> </tr> </table> </form> </body> </html>
<%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Security" %> <%@ Import Namespace="System.Web.UI" %> <script runat="server"> string[] rolesArray; MembershipUserCollection users; public void Page_Load() { Msg.Text = ""; if (!IsPostBack) { // Bind roles to ListBox. rolesArray = Roles.GetAllRoles(); RolesListBox.DataSource = rolesArray; RolesListBox.DataBind(); // Bind users to ListBox. users = Membership.GetAllUsers(); UsersListBox.DataSource = users; UsersListBox.DataBind(); } } public void AddUser_OnClick(object sender, EventArgs args) { // Verify that a user and a role are selected. if (UsersListBox.SelectedItem == null) { Msg.Text = "Please select a user."; return; } if (RolesListBox.SelectedItem == null) { Msg.Text = "Please select a role."; return; } // Add the user to the selected role. try { Roles.AddUserToRole(UsersListBox.SelectedItem.Value, RolesListBox.SelectedItem.Value); Msg.Text = "User added to Role."; } catch (HttpException e) { Msg.Text = e.Message; } } </script> <html> <head> <title>Sample: Role Membership</title> </head> <body> <form runat="server" id="PageForm"> <h3>Role Membership</h3> <asp:Label id="Msg" ForeColor="maroon" runat="server" /><BR> <table CellPadding="3" border="0"> <tr> <td valign="top">Roles:</td> <td valign="top"><asp:ListBox id="RolesListBox" runat="server" Rows="8" /></td> <td valign="top">Users:</td> <td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username" Rows="8" runat="server" /></td> <td valign="top"><asp:Button Text="Add User to Role" id="AddUserButton" runat="server" OnClick="AddUser_OnClick" /></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に収録されているすべての辞書からRoles.AddUserToRole メソッドを検索する場合は、下記のリンクをクリックしてください。

- Roles.AddUserToRole メソッドのページへのリンク