MembershipUser.ChangePassword(String, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
更新成員資格資料存放區中成員資格使用者的密碼。
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
參數
- oldPassword
- String
成員資格使用者的目前密碼。
- newPassword
- String
成員資格使用者的新密碼。
傳回
如果更新成功,則為 true
;否則為 false
。
例外狀況
這個方法無法使用。 如果應用程式以 .NET Framework 4 用戶端配置檔為目標,就會發生這種情況。 若要避免這個例外狀況,請覆寫方法,或將應用程式變更為以完整版本的 .NET Framework 為目標。
範例
下列程式代碼範例會修改目前登入用戶的密碼。
重要
此範例包含接受使用者輸入的文本框,這是潛在的安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@ 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>
備註
ChangePassword ChangePassword會呼叫 屬性所ProviderName參考成員資格提供者的 方法,以更新成員資格數據存放區中成員資格用戶的密碼。
成員資格提供者可能會限制密碼的大小。 如需大小限制,請參閱成員資格提供者的檔。