Wednesday, August 3, 2011

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








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: