Membership.DeleteUser 方法

定義

從資料庫刪除使用者。

多載

DeleteUser(String)

從資料庫刪除使用者和任何相關的使用者資料。

DeleteUser(String, Boolean)

從資料庫刪除使用者。

DeleteUser(String)

從資料庫刪除使用者和任何相關的使用者資料。

public:
 static bool DeleteUser(System::String ^ username);
public static bool DeleteUser (string username);
static member DeleteUser : string -> bool
Public Shared Function DeleteUser (username As String) As Boolean

參數

username
String

要刪除的使用者名稱。

傳回

如果使用者已刪除,則為 true,否則為 false

例外狀況

username 是空字串,或包含逗號 (,)。

usernamenull

範例

下列程式碼範例會刪除目前登入的使用者和所有相關資料。

<%@ 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);
  Response.Redirect("logincs.aspx");
}

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

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

  <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(sender As Object, args As EventArgs)

  Membership.DeleteUser(User.Identity.Name)
  Response.Redirect("loginvb.aspx")

End Sub

Public Sub CancelButton_OnClick(sender As Object, 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>

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

備註

當您使用 SqlRoleProvider 、 和 物件進行資料儲存時,也會刪除儲存在 ProfileRoles 資料庫中的使用者資料、 SqlProfileProviderWebPartSqlPersonalizationProvider 個人化。

另請參閱

適用於

DeleteUser(String, Boolean)

從資料庫刪除使用者。

public:
 static bool DeleteUser(System::String ^ username, bool deleteAllRelatedData);
public static bool DeleteUser (string username, bool deleteAllRelatedData);
static member DeleteUser : string * bool -> bool
Public Shared Function DeleteUser (username As String, deleteAllRelatedData As Boolean) As Boolean

參數

username
String

要刪除的使用者名稱。

deleteAllRelatedData
Boolean

true 表示要從資料庫中刪除與使用者相關的資料,false 表示要保留資料庫中與使用者相關的資料。

傳回

如果使用者已刪除,則為 true,否則為 false

例外狀況

username 是空字串,或包含逗號 (,)。

usernamenull

範例

下列程式碼範例會刪除目前登入的使用者和所有相關資料。

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

備註

從資料庫刪除的使用者只會從設定 applicationName 的 中刪除。

如果 deleteAllRelatedDatatrue ,則當您使用 SqlRoleProvider 、 和 物件進行資料儲存時,也會刪除儲存在 資料庫中 ProfileRoles 的使用者資料、 SqlProfileProviderWebPartSqlPersonalizationProvider 個人化。

另請參閱

適用於