SqlMembershipProvider.DeleteUser(String, Boolean) 方法

定义

从 SQL Server 成员资格数据库删除用户的成员资格信息。Removes a user's membership information from the SQL Server membership database.

public:
 override bool DeleteUser(System::String ^ username, bool deleteAllRelatedData);
public override bool DeleteUser (string username, bool deleteAllRelatedData);
override this.DeleteUser : string * bool -> bool
Public Overrides Function DeleteUser (username As String, deleteAllRelatedData As Boolean) As Boolean

参数

username
String

要删除的用户的名称。The name of the user to delete.

deleteAllRelatedData
Boolean

如果为 true,则从数据库中删除与该用户相关的数据;如果为 false,则将与该用户相关的数据保留在数据库。true to delete data related to the user from the database; false to leave data related to the user in the database.

返回

如果用户已删除,则为 true;否则为 falsetrue if the user was deleted; otherwise, false. 如果数据库中没有此用户,也会返回 false 值。A value of false is also returned if the user does not exist in the database.

异常

username 为空字符串 ("")、包含一个逗号或长度超过 256 个字符。username is an empty string (""), contains a comma, or is longer than 256 characters.

usernamenullusername is null.

示例

下面的代码示例将删除当前已登录的用户。The following code example deletes the currently logged-on user.

备注

此示例使用SqlMembershipProvider defaultProvider在 web.config 文件SqlMembershipProvider中调用指定的作为。This example uses SqlMembershipProvider to call the SqlMembershipProvider specified as the defaultProvider in the Web.config file. 如果需要以类型SqlMembershipProvider访问默认提供程序,则可以Provider强制转换Membership类的属性。If you need to access the default provider as the type SqlMembershipProvider, you can cast the Provider property of the Membership class. 若要以特定的提供程序类型访问其他已配置的提供程序,可以使用Providers Membership类的属性访问这些提供程序,并将其转换为特定的提供程序类型。To access other configured providers as a specific provider type, you can access them by their configured name with the Providers property of the Membership class and cast them as the specific provider type.

<%@ 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">

public void YesButton_OnClick(object sender, EventArgs args)
{
  Membership.DeleteUser(User.Identity.Name, DeleteRelatedData.Checked);

  FormsAuthentication.SignOut();
  FormsAuthentication.RedirectToLoginPage();
}

public void CancelButton_OnClick(object sender, EventArgs args)
{
  Response.Redirect("default.aspx");
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Delete User</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Delete User</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <span style="color:red">Are you sure you want to delete the userid <b><%=User.Identity.Name%></b>?</span><br />

  Delete related profile and roles data: <asp:CheckBox id="DeleteRelatedData" 
                                                       checked="True" runat="Server" /><br />

  <asp:Button id="YesButton" Text="Yes" OnClick="YesButton_OnClick" runat="server" />
  <asp:Button id="CancelButton" Text="Cancel" OnClick="CancelButton_OnClick" runat="server" />
</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">

  Public Sub YesButton_OnClick(ByVal sender As Object, ByVal args As EventArgs)

    Membership.DeleteUser(User.Identity.Name, DeleteRelatedData.Checked)

    FormsAuthentication.SignOut()
    FormsAuthentication.RedirectToLoginPage()

  End Sub

  Public Sub CancelButton_OnClick(ByVal sender As Object, ByVal args As EventArgs)
    Response.Redirect("default.aspx")
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Sample: Delete User</title>
</head>
<body>
  <form id="form1" runat="server">
    <h3>
      Delete User</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <p style="color:red">Are you sure you want to delete the userid <b><%=User.Identity.Name%></b>?</p>
    <br />
      Delete related profile and roles data:
      <asp:CheckBox ID="DeleteRelatedData" Checked="True" runat="Server" /><br />
        <asp:Button ID="YesButton" Text="Yes" OnClick="YesButton_OnClick" runat="server" />
        <asp:Button ID="CancelButton" Text="Cancel" OnClick="CancelButton_OnClick" runat="server" />
  </form>
</body>
</html>

注解

此方法由Membership类调用,以从 ASP.NET 应用程序的配置文件中指定的 SQL Server 数据库中删除用户。This method is called by the Membership class to remove a user from the SQL Server database specified in the ASP.NET application's configuration file.

仅从配置ApplicationName的中删除已删除的用户。Deleted users are only deleted from the configured ApplicationName.

如果deleteAllRelatedData设置为true,则还会为配置WebPart Roles ProfileApplicationName删除、或个性化功能的数据库中存储的所有用户数据。If deleteAllRelatedData is set to true, then all user data stored in the database for the Roles, Profile, or WebPart personalization features is also deleted for the configured ApplicationName.

如果成员资格用户在数据库中不存在,并且deleteAllRelatedDatatrue,则为RolesProfileWebPart个性化功能存储的相关数据和用户信息仍将为在ApplicationName属性中指定的应用程序。If the membership user does not exist in the database, and deleteAllRelatedData is true, then the related data and the user information stored for the Roles, Profile, or WebPart personalization features will still be deleted for the application specified in the ApplicationName property. 因此,该DeleteUser方法可用于快速删除与用户相关的信息,即使该SqlMembershipProvider对象不用于存储经过身份验证的用户信息也是如此。As a result, the DeleteUser method can be used for quickly deleting information related to a user, even when the SqlMembershipProvider object is not used for storing authenticated user information.

删除 username 参数值的前导和尾随空格。Leading and trailing spaces are trimmed from the username parameter value.

适用于

另请参阅