Saturday, May 29, 2010

Custom Forgot Password form for FBA Sharepoint...can be deployed to layouts

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

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Forgot your password? Enter your mail address below, and the password will be mailed to you."></asp:Label><br /><br />
<asp:TextBox ID="txtUserName" Width="300" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox> <br />
<asp:Label ID="lblErrror" runat="server" Text=""></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="Send Password" />
</div>
</form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Mail;

public partial class _Default : System.Web.UI.Page
{

private void AutoUnlockUser(string username)
{
//If user try with wrong user name and password combintaion 5 times he will be locked.
//Using this method he will be unlocked after specified minutes, In this case it is 60.
MembershipUser mu = Membership.GetUser(txtUserName.Text);
if ((mu != null) && (mu.IsLockedOut) && (mu.LastLockoutDate.ToUniversalTime().AddMinutes(60) < DateTime.UtcNow))
{
mu.UnlockUser();
}
}

protected void Page_Load(object sender, EventArgs e)
{

if (IsPostBack)
{
AutoUnlockUser(txtUserName.Text);
MembershipUser user1 = Membership.GetUser(txtUserName.Text);
if (user1 == null)
{
lblErrror.Text = "Sorry, the entered email doent match to any valid user, please try again.";
}
else
{

string newpassword = user1.ResetPassword();


MailMessage objemail = new MailMessage();
objemail.To = "";
objemail.From = "";
objemail.Subject = "";
objemail.Body = "" + newpassword + " . Your username is " +txtUserName.Text +"." ;
objemail.BodyFormat = System.Web.Mail.MailFormat.Html;
objemail.Priority = MailPriority.High;
objemail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
objemail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "emailusername");
objemail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
SmtpMail.SmtpServer = "emailservername";
SmtpMail.Send(objemail);
lblErrror.Text = "Your Password has been mailed to you!";
}
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
}

No comments:

Post a Comment