WebSecurity - GeneratePasswordResetToken()
WebSecurity Object
Definition
GeneratePasswordResetToken()The method generates a password reset token that can be sent to the user in an email so that the user can reset their password.
C# and VB Syntax
WebSecurity.GeneratePasswordResetToken(userName, expiration)
Parameters
| Parameters | Type | Description |
|---|---|---|
| userName | String | User name |
| expiration | Integer | Token expiration time, in minutes. Default is 1440 (24 hours). |
Return Value
| Type | Description |
|---|---|
| String | A reset token. |
Errors and Exceptions
Under the following circumstances, anyWebSecurityaccess to the object will throw anInvalidOperationException:
- InitializeDatabaseConnection()The method has not been called.
- SimpleMembershipNot initialized (or disabled in website configuration).
Remarks
If the user has forgotten their password, use theResetPassword()method.ResetPassword()The method requires a passwordreset token.。
The confirmation token can beCreateAccount()、 CreateUserAndAccount()orGeneratePasswordResetToken()created by the method.
Passwords can be reset via code, but the typical process is to send an email to the user (with the token and a link to a page) so that the user can confirm a new password with the new token:
@{
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>
Technical Data
| Name | Value |
|---|---|
| Namespace | WebMatrix.WebData |
| Assembly | WebMatrix.WebData.dll |
WebSecurity Object Other Extensions