hi to all my friends,
i have created a login system and a password recovery page i used the following code but when i run the application then it is giving me the error that username is not found although the username is already exists in database my sqlserver 2005 is coonected with a proper connection strings please help me out regarding to this my codes are as follows::
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.Secu rity;
public partial class Password_recove ry : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Membership.En ablePasswordRes et)
{
FormsAuthentica tion.RedirectTo LoginPage("Defa ult.aspx");
}
Msg.Text = "";
if (!IsPostBack)
{
Msg.Text = "Please supply a username.";
}
else
{
VerifyUsername( );
}
}
public void VerifyUsername( )
{
MembershipUser u;
u = Membership.GetU ser(UsernameTex tBox.Text, false);
if (u == null)
{
Msg.Text="Usern ame"+ Server.HtmlEnco de(UsernameText Box.Text) +"notfound. Please check the value and re-enter.";
QuestionLabel.T ext = "";
QuestionLabel.E nabled = false;
AnswerTextBox.E nabled = false;
ResetPasswordBu tton.Enabled = false;
}
else
{
QuestionLabel.T ext = u.PasswordQuest ion;
QuestionLabel.E nabled = true;
AnswerTextBox.E nabled = true;
ResetPasswordBu tton.Enabled = true;
}
}
protected void ResetPassword_O nClick(object sender, EventArgs e)
{
MembershipUser u;
string newPassword;
u = Membership.GetU ser(UsernameTex tBox.Text, false);
if (u == null)
{
Msg.Text = "Username " + Server.HtmlEnco de(UsernameText Box.Text) + " not found. Please check the value and re-enter.";
return;
}
try
{
newPassword = u.ResetPassword (AnswerTextBox. Text);
}
catch (MembershipPass wordException ex)
{
Msg.Text = "Invalid password answer. Please re-enter and try again.";
return;
}
catch (Exception ex)
{
Msg.Text = ex.Message;
return;
}
if (newPassword != null)
{
Msg.Text = "Password reset. Your new password is: " + Server.HtmlEnco de(newPassword) ;
}
else
{
Msg.Text = "Password reset failed. Please re-enter your values and try again.";
}
}
}
and its aspx file is::
<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Passw ord-recovery.aspx.c s" Inherits="Passw ord_recovery" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<h3>Retrieve Password</h3>
<asp:Label id="Msg" runat="server" ForeColor="maro on" /><br />
Username: <asp:Textbox id="UsernameTex tBox" Columns="30" runat="server" AutoPostBack="t rue" />
<asp:RequiredFi eldValidator id="UsernameReq uiredValidator" runat="server"
ControlToValida te="UsernameTex tBox" ForeColor="red"
Display="Static " ErrorMessage="R equired" /><br />
Password Question: <b><asp:Label id="QuestionLab el" runat="server" /></b><br />
Answer: <asp:TextBox id="AnswerTextB ox" Columns="60" runat="server" Enabled="false" />
<asp:RequiredFi eldValidator id="AnswerRequi redValidator" runat="server"
ControlToValida te="AnswerTextB ox" ForeColor="red"
Display="Static " ErrorMessage="R equired" Enabled="false" /><br />
<asp:Button id="ResetPasswo rdButton" Text="Reset Password"
OnClick="ResetP assword_OnClick " runat="server" Enabled="false" />
<br />
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="<%$ ConnectionStrin gs:DataBase_Con nection %>"
SelectCommand=" SELECT [id], [user_name], [password], [f_name], [secreat-question] AS column1, [l_name], [secreat-answer] AS column2 FROM [user_account]">
</asp:SqlDataSour ce>
</form>
</body>
</html>