@* Remove this section if you are using bundling *@
@section Scripts {
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Forget Your Password";

    bool passwordSent = false;
    var resetToken = "";
    var email = Request.Form["email"] ?? Request.QueryString["email"];

    // Setup validation
    Validation.RequireField("email", "The email address field is required.");

    if (IsPost) {
        AntiForgery.Validate();
        // validate email
        bool isValid = true;
        if (Validation.IsValid()) {
            if (WebSecurity.GetUserId(email) > -1 && WebSecurity.IsConfirmed(email)) {
                resetToken = WebSecurity.GeneratePasswordResetToken(email); // Optionally specify an expiration date for the token
            } else {
                passwordSent = true; // We don't want to disclose that the user does not exist.
                isValid = false;
            }
        }
        if (isValid) {
            var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
            var resetUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/PasswordReset?resetToken=" + HttpUtility.UrlEncode(resetToken));
            WebMail.Send(
                to: email,
                subject: "Please reset your password", 
                body: "Use this password reset token to reset your password. The token is: " + resetToken + @". Visit <a href=""" + HttpUtility.HtmlAttributeEncode(resetUrl) + @""">" + resetUrl + "</a> to reset your password."
            );
            passwordSent = true;
        }
    }
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
    <h2>Use the form below to reset your password.</h2>
</hgroup>

@if (!WebMail.SmtpServer.IsEmpty()) {
    <p>
        We will send password reset instructions to the email address associated with your account.
    </p>

    if (passwordSent) {
        <p class="message-success">
            Instructions to reset your password have been sent to the specified email address.
        </p>
    }

    <form method="post">
        @AntiForgery.GetHtml()
        @Html.ValidationSummary(excludeFieldErrors: true)

        <fieldset>
            <legend>Password Reset Instructions Form</legend>
            <ol>
                <li class="email">
                    <label for="email" @if (!ModelState.IsValidField("email")) {<text>class="error-label"</text>}>Email address</label>
                    <input type="text" id="email" name="email" value="@email" disabled="@passwordSent" @Validation.For("email") />
                    @Html.ValidationMessage("email")
                </li>
            </ol>
            <p class="form-actions">
                <input type="submit" value="Send instructions" disabled="@passwordSent" />
            </p>
        </fieldset>
    </form>
} else {
   <p class="message-info">
       Password recovery is disabled for this website because the SMTP server is 
       not configured correctly. Please contact the owner of this site to reset 
       your password.
   </p>
}