Share via


ChangePassword.OnChangingPassword(LoginCancelEventArgs) 方法

定义

在成员资格提供程序更改用户的密码之前引发 ChangingPassword 事件。

protected:
 virtual void OnChangingPassword(System::Web::UI::WebControls::LoginCancelEventArgs ^ e);
protected virtual void OnChangingPassword (System.Web.UI.WebControls.LoginCancelEventArgs e);
abstract member OnChangingPassword : System.Web.UI.WebControls.LoginCancelEventArgs -> unit
override this.OnChangingPassword : System.Web.UI.WebControls.LoginCancelEventArgs -> unit
Protected Overridable Sub OnChangingPassword (e As LoginCancelEventArgs)

参数

e
LoginCancelEventArgs

一个包含事件数据的 CancelEventArgs 对象。

示例

下面的代码示例演示如何使用 ASP.NET 页,该页使用 ChangePassword 控件,并包含名为 ChangingPassword的事件的ChangingPassword处理程序。 ChangingPassword 将 存储在 属性中的 CurrentPassword 旧密码与 存储在 中的 NewPassword新密码进行比较。 如果两个密码相同,则更改密码会失败。

控件 ChangePasswordDisplayUserName 属性设置为 true ,使用户能够输入其用户名。 这意味着用户无需登录就可查看页面。

该代码示例假定 ASP.NET 网站已配置为使用 ASP.NET 成员身份和 Forms 身份验证,并且已创建一个名称和密码已知的用户。 有关详细信息,请参阅 如何:实现简单窗体身份验证

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {
    //Manually register the event-handling methods.
    ChangePassword1.ChangingPassword += new LoginCancelEventHandler(this._ChangingPassword);
  }

  void _ChangingPassword(Object sender, LoginCancelEventArgs e)
  {
    if (ChangePassword1.CurrentPassword.ToString() == ChangePassword1.NewPassword.ToString())
    {
      Message1.Visible = true;
      Message1.Text = "Old password and new password must be different.  Please try again.";
      e.Cancel = true;
    }
    else
    {
      //This line prevents the error showing up after a first failed attempt.
      Message1.Visible = false;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true" 
      OnChangingPassword="_ChangingPassword"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!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 PageLoad(ByVal Sender As Object, ByVal e As EventArgs)
    'Manually register the event-handling methods.
    AddHandler ChangePassword1.ChangingPassword, AddressOf Me._ChangingPassword
  End Sub

  Public Sub _ChangingPassword(ByVal Sender As Object, ByVal e As LoginCancelEventArgs)
    If (ChangePassword1.CurrentPassword.ToString() = ChangePassword1.NewPassword.ToString) Then
      Message1.Visible = True
      Message1.Text = "Old password and new password must be different.  Please try again."
      e.Cancel = True
    Else
      'This line prevents the error showing up after a first failed attempt.
      Message1.Visible = False
    End If
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ChangePassword including a ChangingPassword event handler</title>
</head>
<body>
  <form id="form1" runat="server">
  <div style="text-align:center">

    <h1>ChangePassword</h1>
    
    <asp:LoginView ID="LoginView1" Runat="server" 
      Visible="true">
      <LoggedInTemplate>
        <asp:LoginName ID="LoginName1" Runat="server" FormatString="You are logged in as {0}." />
        <br />
      </LoggedInTemplate>
      <AnonymousTemplate>
        You are not logged in
      </AnonymousTemplate>
    </asp:LoginView><br />
    
    <asp:ChangePassword ID="ChangePassword1" Runat="server"
      BorderStyle="Solid" 
      BorderWidth="1" 
      CancelDestinationPageUrl="~/Default.aspx" 
      DisplayUserName="true" 
      OnChangingPassword="_ChangingPassword"
      ContinueDestinationPageUrl="~/Default.aspx" >
    </asp:ChangePassword><br />
  
    <asp:Label ID="Message1" Runat="server" ForeColor="Red" /><br />

    <asp:HyperLink ID="HyperLink1" Runat="server" 
      NavigateUrl="~/Default.aspx">
      Home
    </asp:HyperLink>
    
  </div>
  </form>
</body>
</html>

注解

OnChangingPassword 调用 属性中指定的 MembershipProvider 成员资格提供程序以更改用户密码之前调用 方法。

OnChangingPassword使用 方法在更改密码之前执行任何必要的处理,例如检查新密码以确保它不在常用密码列表中。

方法OnChangingPassword可以通过将作为 e 参数传递的 CancelEventArgs 对象的 属性设置为 Canceltrue取消ChangingPassword事件。

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 ASP.NET Web Forms Pages 中的服务器事件处理

OnChangingPassword 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnChangingPassword(LoginCancelEventArgs) 方法时,请务必为基类调用 OnChangingPassword(LoginCancelEventArgs) 方法,以便注册的委托接收 事件。

适用于

另请参阅