WebSecurity GeneratePasswordResetToken 方法
定義
GeneratePasswordResetToken()方法生成一個密碼重置令牌,可以在電子郵件中發送給用戶以便用戶可以重設密碼。
C# 和VB 語法
WebSecurity.GeneratePasswordResetToken( userName, expiration )
參數
参数 | 类型 | 描述 |
---|---|---|
userName | String | 用户名 |
expiration | Integer | 令牌到期时间,以分钟计。默认是 1440(24 小时) |
返回值
类型 | 描述 |
---|---|
String | 一个重置令牌。 |
錯誤和異常
在下面的情況下,任何對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>
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對象