Updates the password for the membership user in the membership data store.
Namespace:
System.Web.Security
Assembly:
System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Overridable Function ChangePassword ( _
oldPassword As String, _
newPassword As String _
) As Boolean
Dim instance As MembershipUser
Dim oldPassword As String
Dim newPassword As String
Dim returnValue As Boolean
returnValue = instance.ChangePassword(oldPassword, _
newPassword)
public virtual bool ChangePassword(
string oldPassword,
string newPassword
)
public:
virtual bool ChangePassword(
String^ oldPassword,
String^ newPassword
)
public function ChangePassword(
oldPassword : String,
newPassword : String
) : boolean
| Exception | Condition |
|---|
| System..::.ArgumentException |
oldPassword is an empty string. -or-
newPassword is an empty string. |
| System..::.ArgumentNullException |
oldPassword is nullNothingnullptra null reference (Nothing in Visual Basic). -or-
newPassword is nullNothingnullptra null reference (Nothing in Visual Basic). |
ChangePassword calls the ChangePassword method of the membership provider referenced by the ProviderName property to update the password for the membership user in the membership data store.
The membership provider may have restrictions on the size of the password. For size limitations, see the documentation for the membership provider.
The following code example modifies the password for the current logged-on user.
Security Note: |
|---|
This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview. |
<%@ 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 >
<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="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 >
<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>
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference
Other Resources