20 September 2013

Password Recovery (A Simple Solution in C#)



My simple solution in recovering passwords without sending it via SMTP. I prefer this approach for some security reasons....

 
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Password Recovery</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>
        Password Recovery</h1>
    <br />
    <asp:PasswordRecovery ID="recoverPassword" runat="server" Style="font-size: small;
        font-family: Tahoma; color: #666666;" OnSendingMail="recoverPassword_SendingMail">
        <MailDefinition From="admin@admin.com" Priority="High" BodyFileName="~/ForgotPassword.txt">
        </MailDefinition>
        <UserNameTemplate>
            <table cellpadding="1" cellspacing="0" style="border-collapse: collapse;">
                <tr>
                    <td>
                        <table cellpadding="0">
                            <tr>
                                <td align="center" colspan="2">
                                    Enter User Name to create a new password.<br />
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="UserName" runat="server" Style="font-size: medium; font-family: Tahoma"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                        ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="recoverPassword">*</asp:RequiredFieldValidator>
                                    <br />
                                </td>
                            </tr>
                            <tr>
                                <td align="center" colspan="2" style="color: Red;">
                                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" colspan="2">
                                    <br />
                                    <asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="Generate New Password"
                                        ValidationGroup="recoverPassword" Style="font-size: small; font-family: Tahoma"
                                        BackColor="#666666" BorderColor="#666666" BorderStyle="Solid" ForeColor="White" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </UserNameTemplate>
    </asp:PasswordRecovery>
    </form>
</body>
</html>

- Code-Behind -

using System;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void recoverPassword_SendingMail(object sender, MailMessageEventArgs e)
    {
        e.Cancel = true;
        recoverPassword.SuccessText = e.Message.Body;
    }
}
 

No comments:

Post a Comment