AuthorizationStoreRoleProvider.DeleteRole(String, Boolean) 方法     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从授权管理器策略存储区中移除角色。
public:
 override bool DeleteRole(System::String ^ roleName, bool throwOnPopulatedRole);public override bool DeleteRole(string roleName, bool throwOnPopulatedRole);override this.DeleteRole : string * bool -> boolPublic Overrides Function DeleteRole (roleName As String, throwOnPopulatedRole As Boolean) As Boolean参数
- roleName
- String
要删除的角色的名称。
- throwOnPopulatedRole
- Boolean
如果为 true,则当 roleName 包含一个或多个成员时将引发异常。
返回
如果角色已删除,则为 true;否则为 false。
例外
              roleName 为 null。
              roleName 具有一个或多个成员且 throwOnPopulatedRole 为 true。
- 或 -
未找到已配置的 applicationName。
- 或 -
未找到已配置的 scopeName。
- 或 -
服务器上没有安装身份验证管理器运行时。
              connectionStringName 特性引用了一个指向不存在的文件的连接字符串。
AuthorizationStoreRoleProvider 实例使用基于文件的策略存储进行配置,且当前的信任级别不允许对文件进行读访问。
示例
以下示例从授权管理器策略存储中删除角色。 有关启用角色管理的 Web.config 文件的示例,请参阅 AuthorizationStoreRoleProvider。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!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;
  public void Page_Load(object sender, EventArgs args)
  {
    if (!IsPostBack)
    {
      // Bind roles to ListBox.
      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;
      RolesListBox.DataBind();
    }
  }
  public void DeleteRole_OnClick(object sender, EventArgs args)
  {
    string delRole = "";
    try
    {
      delRole = RolesListBox.SelectedItem.Value;
      Roles.DeleteRole(delRole);
      Msg.Text = "Role '" + Server.HtmlEncode(delRole) + "' deleted.";
      // Re-bind roles to ListBox.
      rolesArray = Roles.GetAllRoles();
      RolesListBox.DataSource = rolesArray;
      RolesListBox.DataBind();
    }
    catch
    {
      Msg.Text = "Role '" + Server.HtmlEncode(delRole) + "' <u>not</u> deleted.";
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample: Delete Role</title>
</head>
<body>
  <form runat="server" id="PageForm">
    <h3>
      Delete Role</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table border="0">
      <tr>
        <td valign="top">
          Delete Role:</td>
        <td valign="top">
          <asp:ListBox ID="RolesListBox" runat="server" Rows="8" /></td>
        <td valign="top">
          <asp:Button Text="Delete Role" ID="DeleteRoleButton" runat="server" OnClick="DeleteRole_OnClick" /></td>
      </tr>
    </table>
  </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!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
  Public Sub Page_Load(ByVal sender As Object, ByVal args As EventArgs)
    If Not IsPostBack Then
      ' Bind roles to ListBox.
      rolesArray = Roles.GetAllRoles()
      RolesListBox.DataSource = rolesArray
      RolesListBox.DataBind()
    End If
  End Sub
  Public Sub DeleteRole_OnClick(ByVal sender As Object, ByVal args As EventArgs)
    Dim delRole As String
    Try
      delRole = RolesListBox.SelectedItem.Value
      Roles.DeleteRole(delRole)
      Msg.Text = "Role '" & Server.HtmlEncode(delRole) & "' deleted."
      ' Re-bind roles to ListBox.
      rolesArray = Roles.GetAllRoles()
      RolesListBox.DataSource = rolesArray
      RolesListBox.DataBind()
    Catch
      Msg.Text = "Role '" & Server.HtmlEncode(delRole) & "' <u>not</u> deleted."
    End Try
  End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample: Delete Role</title>
</head>
<body>
  <form runat="server" id="PageForm">
    <h3>
      Delete Role</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <table border="0">
      <tr>
        <td valign="top">
          Delete Role:</td>
        <td valign="top">
          <asp:ListBox ID="RolesListBox" runat="server" Rows="8" /></td>
        <td valign="top">
          <asp:Button Text="Delete Role" ID="DeleteRoleButton" runat="server" OnClick="DeleteRole_OnClick" /></td>
      </tr>
    </table>
  </form>
</body>
</html>
注解
类 DeleteRole 调用 Roles 方法,以从 ASP.NET 应用程序的配置文件 (Web.config) 中指定的授权管理器策略存储中删除角色。 删除角色时,也会从策略存储中删除与该角色关联的用户列表。 数据库中的用户信息不受影响。
如果 throwOnPopulatedRole 为 true,则将引发异常,如果 参数标识的角色具有一个或多个成员,则不会删除该 roleName 角色。 如果 throwOnPopulatedRole 为 false,则无论角色是否为空,都将删除该角色。