Latest web development tutorials

WebSecurity ResetPassword 方法

WebSecurity 對象 WebSecurity對象

定義

ResetPassword()方法使用密碼令牌重置用戶密碼。


C# 和VB 語法

WebSecurity.ResetPassword( passwordResetToken,newPassword)


參數

参数 类型 描述
passwordResetToken String 密码令牌
newpassword String 新密码


返回值

类型 描述
Boolean 如果密码已更改,则返回 true ,否则返回 false


錯誤和異常

在下面的情況下,任何對WebSecurity對象的訪問將拋出一個InvalidOperationException

  • InitializeDatabaseConnection()方法沒有被調用
  • SimpleMembership沒有初始化(或者在網站配置中禁用)

備註

如果用戶已忘記密碼,請使用ResetPassword方法。

ResetPassword方法要求一個密碼重置令牌

確認令牌可通過CreateAccount()CreateUserAndAccount()GeneratePasswordResetToken()方法創建。

密碼可通過代碼重置,但是一般過程是發送電子郵件給用戶(帶有令牌和指向頁面的鏈接),這樣用戶就可以通過新的令牌確認新的密碼:

@{
newPassword = Request["newPassword"];
confirmPassword = Request["confirmPassword"];
token = Request["token"];
if isPost
{
// input testing is ommitted here to save space
retunValue = ResetPassword(token, newPassword);
}
}
<h1>Change Password</h1>

<form method="post" action="">

<label for="newPassword">New Password:</label>
<input type="password" id="newPassword" name="newPassword" title="New password" />

<label for="confirmPassword">Confirm Password:</label>
<input type="password" id="confirmPassword" name="confirmPassword" title="Confirm new password" />

<label for="token">Pasword Token:</label>
<input type="text" id="token" name="token" title="Password Token" />

<p class="form-actions">
<input type="submit" value="Change Password" title="Change password" />
</p>

</form>


技術數據

名称
Namespace WebMatrix.WebData
Assembly WebMatrix.WebData.dll


WebSecurity 對象 WebSecurity對象