Roles.AddUserToRole(String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的使用者加入指定的角色。
public:
static void AddUserToRole(System::String ^ username, System::String ^ roleName);
public static void AddUserToRole (string username, string roleName);
static member AddUserToRole : string * string -> unit
Public Shared Sub AddUserToRole (username As String, roleName As String)
參數
- username
- String
要加入指定角色的使用者名稱。
- roleName
- String
要加入指定使用者名稱的角色。
例外狀況
範例
下列程式代碼範例會將使用者新增至角色。 如需啟用角色管理的 Web.config 檔案範例,請參閱 Roles。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<!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;
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 xmlns="http://www.w3.org/1999/xhtml" >
<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="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<!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
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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
備註
方法 AddUserToRole 會呼叫預設角色提供者,以將指定的使用者與數據源上的指定角色產生關聯。
注意
用戶名稱和角色名稱不能包含逗號。