Redigera

Dela via


SqlMembershipProvider.DeleteUser(String, Boolean) Method

Definition

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

Parameters

username
String

The name of the user to delete.

deleteAllRelatedData
Boolean

true to delete data related to the user from the database; false to leave data related to the user in the database.

Returns

true if the user was deleted; otherwise, false. A value of false is also returned if the user does not exist in the database.

Exceptions

username is an empty string (""), contains a comma, or is longer than 256 characters.

username is null.

Examples

The following code example deletes the currently logged-on user.

Note

This example uses SqlMembershipProvider to call the SqlMembershipProvider specified as the defaultProvider in the Web.config file. If you need to access the default provider as the type SqlMembershipProvider, you can cast the Provider property of the Membership class. 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>

Remarks

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.

Deleted users are only deleted from the configured ApplicationName.

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.

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. 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.

Leading and trailing spaces are trimmed from the username parameter value.

Applies to

See also