在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
首先要配置web.config 1. 确保enablePasswordRetrieval="true" requiresQuestionAndAnswer="true" passwordFormat="Encrypted" <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" passwordFormat="Encrypted" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" maxInvalidPasswordAttempts="15" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/> </providers> </membership>
2. 在<system.web>内加入machineKey的配置,用于加密,在线生成machineKey的网站 http://aspnetresources.com/tools/machineKey
3. 在<system.net> 中配置<mailSettings>用于发送邮件给找回密码的客户,发送到他注册的邮箱。 <system.net> <mailSettings> <smtp deliveryMethod="Network" from="[email protected]"> <network host="smtp.163.com" userName="testemail" password="*****" port="25" defaultCredentials="false" /> </smtp> </mailSettings> </system.net>
4. 编写RetrievePassword.aspx页面用于找回密码,下面是RetrievePassword.aspx页面代码 Content1" ContentPlaceHolderID="ContentPlaceHolderMain" Runat="Server"> <div > <div class="mt"> <h2>找回密码</h2> <b></b> </div> <div style="padding-top:20px;" class="mc"> <asp:PasswordRecovery ID="PasswordRecovery" runat="server" UserNameFailureText="用户名不存在或无法验证, 请重新输入." SuccessText="您的密码已经发送给您的注册邮箱,请尽快查收!" QuestionFailureText="您填写的答案不正确, 请重试." onsendmailerror="PasswordRecovery_SendMailError" onsendingmail="PasswordRecovery_SendingMail"> <UserNameTemplate> <div class="form"> <div style="width:480px;height:21px;line-height:21px;padding:2px 4px;margin:0 auto 20px;" class="ac flk13 ftx24">忘记了密码?请先输入您的用户名!</div> <div class="item"> <span class="label">用户名:</span> <div class="fl"> <asp:TextBox CssClass="text" ID="UserName" runat="server"></asp:TextBox> <%--<label class="blank invisible" ></label>--%> <span class="clr"></span> <asp:RequiredFieldValidator ID="UserNameRequired" CssClass="error" runat="server" ControlToValidate="UserName" ErrorMessage="请填写用户名." ToolTip="请填写用户名." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> </div> </div> <div class="item"> <span class="label"> </span> <asp:Button ID="SubmitButton" class="btn-img btn-entry" runat="server" CommandName="Submit" Text="下一步" ValidationGroup="PasswordRecovery1" /> </div> <div class="item"> <span class="label"> </span> <span class="error"><asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></span> </div> </div> </UserNameTemplate> <QuestionTemplate> <div class="form"> <div style="width:480px;height:21px;line-height:21px;padding:2px 4px;margin:0 auto 20px;" class="ac flk13 ftx24">您输入的用户名有效,请继续输入注册时填写的问题答案,验证成功后我们会把重设密码邮件发送到您的邮箱!</div> <div class="item"> <span class="label">用户名:</span> <div class="fl"> <asp:TextBox ID="UserName" CssClass="text" ReadOnly="true" runat="server"></asp:TextBox> <%-- <asp:Literal ID="1" runat="server"></asp:Literal>--%> </div> </div> <div class="item"> <span class="label">提示问题:</span> <div class="fl"> <asp:TextBox ID="Question" CssClass="text" ReadOnly="true" runat="server"></asp:TextBox> </div> </div> <div class="item"> <span class="label">问题答案:</span> <div class="fl"> <asp:TextBox ID="Answer" CssClass="text" runat="server"></asp:TextBox> <asp:RequiredFieldValidator CssClass="error" ID="AnswerRequired" runat="server" ControlToValidate="Answer" ErrorMessage="请填写问题答案." ToolTip="请填写问题答案." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator> <span class="clr"></span> </div> </div> <div class="item"> <span class="label"> </span> <asp:Button ID="SubmitButton" runat="server" class="btn-img btn-entry" CommandName="Submit" Text="下一步" ValidationGroup="PasswordRecovery1" /> </div> <div class="item"> <span class="label"> </span> <span class="error"><asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></span> </div> </div> </QuestionTemplate> <SuccessTemplate> <div >您的密码已经发送到您的注册邮箱,请尽快查收!</div> <div class="item"> <span class="label"> </span> <span class="error"><asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></span> </div> </SuccessTemplate> </asp:PasswordRecovery> <span class="error"><asp:Literal ID="info" runat="server" /></span> </div> </div> </asp:Content>
下面是RetrievePassword.aspx.cs 代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class RetrievePassword : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void PasswordRecovery_SendMailError(object sender, SendMailErrorEventArgs e) { this.info.Text = e.Exception.Message; ClientScriptManager csm1 = this.ClientScript; e.Handled = true; //将这个异常设置为已经处理 } protected void PasswordRecovery_SendingMail(object sender, MailMessageEventArgs e) { System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(this.PasswordRecovery.UserName); e.Message.Body = string.Format(System.IO.File.ReadAllText(Server.MapPath("FindPasswordTemplate.htm")), this.PasswordRecovery.UserName, this.PasswordRecovery.UserName, user.GetPassword(this.PasswordRecovery.Answer), System.Web.Configuration.WebConfigurationManager.AppSettings["Domain"], System.Web.Configuration.WebConfigurationManager.AppSettings["Domain"], DateTime.Now); e.Message.IsBodyHtml = true; } } |
请发表评论