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.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class Password_recovery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Membership.EnablePasswordReset)
{
FormsAuthentication.RedirectToLoginPage("Default.a spx");
}
Msg.Text = "";
if (!IsPostBack)
{
Msg.Text = "Please supply a username.";
}
else
{
VerifyUsername();
}
}
public void VerifyUsername()
{
MembershipUser u;
u = Membership.GetUser(UsernameTextBox.Text, false);
if (u == null)
{
Msg.Text="Username"+ Server.HtmlEncode(UsernameTextBox.Text) +"notfound. Please check the value and re-enter.";
QuestionLabel.Text = "";
QuestionLabel.Enabled = false;
AnswerTextBox.Enabled = false;
ResetPasswordButton.Enabled = false;
}
else
{
QuestionLabel.Text = u.PasswordQuestion;
QuestionLabel.Enabled = true;
AnswerTextBox.Enabled = true;
ResetPasswordButton.Enabled = true;
}
}
protected void ResetPassword_OnClick(object sender, EventArgs e)
{
MembershipUser u;
string newPassword;
u = Membership.GetUser(UsernameTextBox.Text, false);
if (u == null)
{
Msg.Text = "Username " + Server.HtmlEncode(UsernameTextBox.Text) + " not found. Please check the value and re-enter.";
return;
}
try
{
newPassword = u.ResetPassword(AnswerTextBox.Text);
}
catch (MembershipPasswordException 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.HtmlEncode(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="Password-recovery.aspx.cs" Inherits="Password_recovery" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<h3>Retrieve Password</h3>
<asp:Label id="Msg" runat="server" ForeColor="maroon" /><br />
Username: <asp:Textbox id="UsernameTextBox" Columns="30" runat="server" AutoPostBack="true" />
<asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UsernameTextBox" ForeColor="red"
Display="Static" ErrorMessage="Required" /><br />
Password Question: <b><asp:Label id="QuestionLabel" runat="server" /></b><br />
Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="false" />
<asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
ControlToValidate="AnswerTextBox" ForeColor="red"
Display="Static" ErrorMessage="Required" Enabled="false" /><br />
<asp:Button id="ResetPasswordButton" Text="Reset Password"
OnClick="ResetPassword_OnClick" runat="server" Enabled="false" />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DataBase_Connection %>"
SelectCommand="SELECT [id], [user_name], [password], [f_name], [secreat-question] AS column1, [l_name], [secreat-answer] AS column2 FROM [user_account]">
</asp:SqlDataSource>
</form>
</body>
</html>