• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET通过回答提示问题以邮件方式找回密码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先要配置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">&nbsp;</span>
                <asp:Button ID="SubmitButton" class="btn-img btn-entry" runat="server" CommandName="Submit" Text="下一步" ValidationGroup="PasswordRecovery1" />
			</div>
            <div class="item">
				<span class="label">&nbsp;</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">&nbsp;</span>
                <asp:Button ID="SubmitButton" runat="server" class="btn-img btn-entry" CommandName="Submit" Text="下一步" ValidationGroup="PasswordRecovery1" />
			</div>
            <div class="item">
				<span class="label">&nbsp;</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">&nbsp;</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;
    }
}

 

功能页面展示:

 

 

 

 

 

 

 

 

 

 

 

用户名和提示问题是绑定到textbox的,只读显示,用户只是要输入答案:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

打开163邮箱,收到了,耶!

 

 


 

 

 

 

 

 


 



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap