MembershipUser.ChangePassword(String, String) Méthode

Définition

Met à jour le mot de passe pour l’utilisateur d’appartenance dans le magasin de données d’appartenance.

public:
 virtual bool ChangePassword(System::String ^ oldPassword, System::String ^ newPassword);
public virtual bool ChangePassword (string oldPassword, string newPassword);
abstract member ChangePassword : string * string -> bool
override this.ChangePassword : string * string -> bool
Public Overridable Function ChangePassword (oldPassword As String, newPassword As String) As Boolean

Paramètres

oldPassword
String

Mot de passe actuel de l’utilisateur d’appartenance.

newPassword
String

Nouveau mot de passe de l’utilisateur d’appartenance.

Retours

Boolean

true si la mise à jour a réussi ; sinon, false.

Exceptions

oldPassword est une chaîne vide.

  • ou -

newPassword est une chaîne vide.

oldPassword a la valeur null.

  • ou -

newPassword a la valeur null.

Cette méthode n’est pas disponible. Cela peut se produire si l’application cible le profil client .NET Framework 4. Pour éviter cette exception, remplacez la méthode ou modifiez l’application pour qu’elle cible la version complète du .NET Framework.

Exemples

L’exemple de code suivant modifie le mot de passe de l’utilisateur connecté actuel.

Important

Cet exemple contient une zone de texte qui accepte l’entrée utilisateur, qui est une menace de sécurité potentielle. Par défaut, les pages web ASP.NET vérifient que l’entrée d’utilisateur n’inclut pas de script ou d’éléments HTML. Pour plus d’informations, consultez Vue d’ensemble des attaques de script.

<%@ 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 ChangePassword_OnClick(object sender, EventArgs args)
{
  // Update the password.

  MembershipUser u = Membership.GetUser(User.Identity.Name);

  try
  {
    if (u.ChangePassword(OldPasswordTextbox.Text, PasswordTextbox.Text))
    {
      Msg.Text = "Password changed.";
    }
    else
    {
      Msg.Text = "Password change failed. Please re-enter your values and try again.";
    }
  }
  catch (Exception e)
  {
    Msg.Text = "An exception occurred: " + Server.HtmlEncode(e.Message) + ". Please re-enter your values and try again.";
  }
}


</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Change Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Change Password for <%=User.Identity.Name%></h3>

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Old Password:</td>
      <td><asp:Textbox id="OldPasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="OldPasswordRequiredValidator" runat="server"
                                      ControlToValidate="OldPasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
                                      ControlToValidate="PasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Confirm Password:</td>
      <td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" />
          <asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" ControlToCompare="PasswordTextBox"
                                      ErrorMessage="Confirm password must match password." />
      </td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="ChangePasswordButton" Text="Change Password" 
                      OnClick="ChangePassword_OnClick" runat="server" /></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">

Public Sub ChangePassword_OnClick(sender As Object, args As EventArgs)
  ' Update the password.

  Dim u As MembershipUser = Membership.GetUser(User.Identity.Name)

  Try
    If u.ChangePassword(OldPasswordTextbox.Text, PasswordTextbox.Text) Then
      Msg.Text = "Password changed."
    Else
      Msg.Text = "Password change failed. Please re-enter your values and try again."
    End If
  Catch e As Exception
    Msg.Text = "An exception occurred: " & Server.HtmlEncode(e.Message) & ". Please re-enter your values and try again."
  End Try

End Sub


</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Change Password</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Change Password for <%=User.Identity.Name%></h3>

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

  <table cellpadding="3" border="0">
    <tr>
      <td>Old Password:</td>
      <td><asp:Textbox id="OldPasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="OldPasswordRequiredValidator" runat="server"
                                      ControlToValidate="OldPasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Password:</td>
      <td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
                                      ControlToValidate="PasswordTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" /></td>
    </tr>
    <tr>
      <td>Confirm Password:</td>
      <td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
      <td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" />
          <asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
                                      ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
                                      Display="Static" ControlToCompare="PasswordTextBox"
                                      ErrorMessage="Confirm password must match password." />
      </td>
    </tr>
    <tr>
      <td></td>
      <td><asp:Button id="ChangePasswordButton" Text="Change Password" 
                      OnClick="ChangePassword_OnClick" runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

Remarques

ChangePassword appelle la ChangePassword méthode du fournisseur d’appartenance référencée par la ProviderName propriété pour mettre à jour le mot de passe de l’utilisateur d’appartenance dans le magasin de données d’appartenance.

Le fournisseur d’appartenances peut avoir des restrictions sur la taille du mot de passe. Pour connaître les limitations de taille, consultez la documentation du fournisseur d’appartenances.

S’applique à

Voir aussi