How to set remember me checkbox to store the username and password in login page?

Posted by Prabu_Spark under ASP.NET on 12/19/2012 | Points: 10 | Views : 4071 | Status : [Member] | Replies : 2
Hi sir,
How to set the remember me checkbox to store the username and password in login page. Kindly give me the
solution for this problem.

[Note: If check the Remember me checkbox, it will not ask the user to ask the username and passwword
while logging next time]

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Ankitsrist on: 12/19/2012 [Member] Starter | Points: 25

Up
0
Down
yeah i also wanted to know this

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: aswinialuri-19361 on: 12/19/2012 [Member] Starter | Points: 25

Up
0
Down
Hi
Let us see how to do this

Add code below in Default.aspx

<table>
<tr>
<td colspan="2" style="text-align: center; height: 21px;">
<span style="font-size: 24pt">Login</span>
</td>
</tr>
<tr>
<td style="width: 71px" valign="top">
<asp:Label ID="lblEmail" runat="server" Text="Email:"></asp:Label></td>
<td style="width: 178px; text-align: right;" valign="top">
<asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td style="width: 71px; height: 26px;">
<asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label></td>
<td style="width: 178px; height: 26px; text-align: right;">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align: right">
<asp:CheckBox ID="ChkRemember" runat="server" Text="Remember Me" /></td>
</tr>
<tr>
<td style="width: 71px; height: 27px;">
</td>
<td style="width: 178px; text-align: right; height: 27px;">
<asp:Button ID="btnLogin" runat="server" Style="position: relative"
Text="Login" onclick="btnLogin_Click" /></td>
</tr>
</table>
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>

Stored procedure used to verify account

CREATE PROCEDURE dbo.VerifyAccount
@Email Varchar(100),
@Password NVarchar(50),
@Verify BIT OUTPUT

AS

BEGIN

DECLARE @RecordCount INT

SELECT @RecordCount = Count(*) FROM Account WHERE Email = @Email AND Password = @Password

IF @RecordCount = 1
BEGIN
SET @Verify = 1
END
ELSE
BEGIN
SET @Verify = 0
END

END


Open code behind file and include following namespace.

C#

using System.Data;
using System.Data.SqlClient;
Public bool
{
string connstring = "Data Source=Local;Initial Catalog=SampleApplicationDatabase;Integrated Security=True";
SqlConnection conn = new SqlConnection(connstring);

SqlCommand cmd = new SqlCommand("VerifyAccount", conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Email", SqlDbType.VarChar, 100).Value = email;
cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = password;

SqlParameter pVerify = new SqlParameter("@Verify", SqlDbType.Bit);
pVerify.Direction = ParameterDirection.InputOutput;
pVerify.Value = DBNull.Value;

cmd.Parameters.Add(pVerify);

try
{
conn.Open();
cmd.ExecuteNonQuery();

if ((bool)pVerify.Value == true)
{
result = true;
}
else
{
result = false;
}
}
catch (Exception ex)
{
result = false;
}
conn.Close();

return result;
}


protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Remember"] != null)
{
HttpCookie cookieRemember = Request.Cookies.Get("Remember");
txtEmail.Text = cookieRemember.Values["Email"];
txtPassword.Text = cookieRemember.Values["Password"];
Response.Cookies["Remember"].Expires = DateTime.Now;
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
bool result = VerifyAccount(txtEmail.Text, txtPassword.Text);

if (result == true)
{
if (ChkRemember.Checked)
{
HttpCookie cookieRemember = new HttpCookie("Remember");
cookieRemember.Values.Add("Email", txtEmail.Text);
cookieRemember.Values.Add("Password", txtPassword.Text);
cookieRemember.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(cookieRemember);
}
Response.Redirect("UserPage.aspx");
}
else
{
lblMessage.Text = "Sorry you cannot login, please provide correct Email and password";
}
}



If you can't get it means you will raise a query I will explain that .

Thanks & regards
Aswini Aluri


Mark as Answer if it helps you
Thanks&Regards
Aswini Aluri

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response