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

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

Roles.AddUserToRole メソッド

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

指定されたユーザーを指定されたロールに追加します。

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

Public Shared Sub AddUserToRole
 ( _
    username As String, _
    roleName As String _
)
Dim username As String
Dim roleName As String

Roles.AddUserToRole(username, roleName)
public static void AddUserToRole
 (
    string username,
    string roleName
)
public:
static void AddUserToRole (
    String^ username, 
    String^ roleName
)
public static void AddUserToRole
 (
    String username, 
    String roleName
)
public static function AddUserToRole
 (
    username : String, 
    roleName : String
)

パラメータ

username

指定されたロールに追加するユーザー名。

roleName

指定されたユーザー名を追加するロール。

例外例外
例外の種類条件

System.ArgumentNullException

roleName が null 参照 (Visual Basic では Nothing) です。

または

username が null 参照 (Visual Basic では Nothing) です。

System.ArgumentException

roleName が空の文字列であるか、またはコンマ (,) を含んでいます。

または

username が空の文字列であるか、またはコンマ (,) を含んでいます。

System.Configuration.Provider.ProviderException

ロール管理は有効になっていません。

解説解説
使用例使用例

ユーザーをロールに追加するコード例を次に示します。ロール管理を有効にする 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>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「Roles.AddUserToRole メソッド」の関連用語

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

   

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



Roles.AddUserToRole メソッドのページの著作権

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

©2026 GRAS Group, Inc.RSS