.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

how to use remember me option in login control in asp.net

Posted By: priya     Posted Date: February 23, 2009    Points:2   Category :ASP.Net
Hello Friends,
how to use remember me option in login control in asp.net. If user check the Remember me options then next time user name and password should be available in the login control.

I am using C# as code behind.

Responses
Author: Athira Sreejith             
Posted Date: February 23, 2009     Points: 5   

Hi..

Refer:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["myCookie"] != null)
{
HttpCookie cookie = Request.Cookies.Get("myCookie");
txtUserName.Text = cookie.Values["username"];
txtPassword.Text = cookie.Values["password"];
}
}
}

protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
objImpl = new WebImplIPhone();
bool IsAvailable = false;
HttpCookie myCookie = new HttpCookie("myCookie");
bool IsRemember = chkRememberMe.Checked;

IsAvailable = objImpl.CheckUserLogin(txtUserName.Text, txtPassword.Text);
if (IsAvailable)
{
DataTable dtUserName = objImpl.ReadUserIdbyUserName(txtUserName.Text);
if (dtUserName != null)
{
if (dtUserName.Rows.Count == 1)
{
Session["UserId"] = dtUserName.Rows[0].ItemArray[0].ToString();
}
}
if (IsRemember)
{
myCookie.Values.Add("username", txtUserName.Text);
myCookie.Values.Add("password", txtPassword.Text);
myCookie.Expires = DateTime.Now.AddDays(15);
}
else
{
myCookie.Values.Add("username", string.Empty);
myCookie.Values.Add("password", string.Empty);
myCookie.Expires = DateTime.Now.AddMinutes(5);
}
Response.Cookies.Add(myCookie);
//FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, chkRememberMe.Checked);
Response.Redirect("ApplicationList.aspx");
}
else
{
lblError.Text = "Invalid UserName or Password or else your Username blocked";
}
}
catch (Exception ex)
{
Response.Redirect("Home.aspx");
}
}

Author: Suresh             
Posted Date: February 23, 2009     Points: 5   

hi Athira Appukuttan

I have bit doubt in above code. my doubts below. whts webimliphone();
can u plz explain it clearly. can u send the code to my id suresh_2614@yahoo.co.in
objImpl = new WebImplIPhone();
bool IsRemember = chkRememberMe.Checked;

IsAvailable = objImpl.CheckUserLogin(txtUserName.Text, txtPassword.Text);

Author: Athira Sreejith             
Posted Date: February 23, 2009     Points: 5   

Hi..

WebImplIPhone is a classfile,which contain db connection and methods.

bool IsRemember = chkRememberMe.Checked;
This line will return checkbox value.that is if checkbox is checked it will return true otherwise false..

CheckUserLogin_- is a method in Class file ,checking the username and password is exist or not.Also it will return bool value.Parameters are uname and password.In that function,correspoding that username and pwd checking whether record is exisit or not.It its it will return True,otherwise False.

Author: Suresh             
Posted Date: February 23, 2009     Points: 5   

hi Athira Appukuttan


can u send class file code too. its very useful to beginner of asp.net, to know about these things. Thank you for ur nice explanation

Author: Athira Sreejith             
Posted Date: February 24, 2009     Points: 5   

Try like this..

protected void btnLogin_Click(object sender, ImageClickEventArgs e)
{
LearnHelper objLogin = new LearnHelper();
bool IsAvailable = false;
HttpCookie myCookie = new HttpCookie("myCookie");
bool IsRemember = chkRememberme.Checked;
IsAvailable = objLogin.CheckUserLogin(txtUsername.Text.Trim(), txtPassword.Text.Trim());
if (IsAvailable)
{
DataSet dsData = objLogin.CheckUserAvailablity(txtUsername.Text.Trim(), txtPassword.Text.Trim());
if (dsData.Tables[0].Rows.Count > 0)
{
Session["Username"] = txtUsername.Text.Trim();
Session["UserId"] = dsData.Tables[0].Rows[0].ItemArray[0].ToString();
}
if (IsRemember)
{
myCookie.Values.Add("username", txtUsername.Text);
myCookie.Values.Add("password", txtPassword.Text);
myCookie.Expires = DateTime.Now.AddDays(15);
Response.Cookies.Add(myCookie);
Response.Redirect("Dashboard.aspx");
}
else
{
myCookie.Values.Add("username", string.Empty);
myCookie.Values.Add("password", string.Empty);
myCookie.Expires = DateTime.Now.AddMinutes(5);
Response.Cookies.Add(myCookie);
}
}
else
{
lblMessage.Text = "Invalid login details or else your account has been blocked!";
}
}

-------------------
LearnHelper.cs:
===============================

using System;
using System.Data;
using System.Configuration;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;

namespace Learn
{
public class LearnHelper
{
string strSuccess = string.Empty;

public DataSet CheckUserAvailablity(string strUname, string strPwd)
{
SQLHelper sqlHelper = new SQLHelper();
SqlParameter sp1 = new SqlParameter("@UserName", strUname);
SqlParameter sp2 = new SqlParameter("@Password", strPwd);
DataSet dsData = sqlHelper.ExecuteDataSetSPDB("CheckUserloginAvailablity", new SqlParameter[] { sp1, sp2 });
return dsData;
}

public bool CheckUserNameAvailablity(string strUsername)
{
SQLHelper objHelp = new SQLHelper();
bool IsSuccess = false;
SqlParameter p1 = new SqlParameter("@UserName", strUsername);
DataSet dsUser = objHelp.ExecuteDataSetSPDB("CheckUserNameAvailablity", new SqlParameter[] { p1 });
if (dsUser.Tables[0].Rows.Count > 0)
{
IsSuccess = true;
}
return IsSuccess;
}

public bool CheckUserLogin(string strUsername, string strPassword)
{
SQLHelper objCheck = new SQLHelper();
bool IsAvailable = false;
SqlParameter sp1 = new SqlParameter("@UserName", strUsername);
SqlParameter sp2 = new SqlParameter("@Password", strPassword);
DataSet ds = objCheck.ExecuteDataSetSPDB("CheckUserLogin", new SqlParameter[] { sp1, sp2 });
if (ds.Tables[0].Rows.Count > 0)
{
IsAvailable = true;
}
return IsAvailable;
}

public string CreateNewUser(string UserName, string Password, string RePassword, string FirstName, string LastName, string Email)
{
SQLHelper objAdd = new SQLHelper();
bool IsSuccess = false;
SqlParameter sp1 = new SqlParameter("@UserName", UserName);
SqlParameter sp2 = new SqlParameter("@Password", Password);
SqlParameter sp3 = new SqlParameter("@RePassword", RePassword);
SqlParameter sp4 = new SqlParameter("@FirstName", FirstName);
SqlParameter sp5 = new SqlParameter("@LastName", LastName);
SqlParameter sp6 = new SqlParameter("@Email", Email);
IsSuccess = objAdd.ExecuteNonQuerySPDB("UpdateRegisteration", new SqlParameter[] { sp1, sp2, sp3, sp4, sp5, sp6 });
if (IsSuccess)
{
strSuccess = "Inserted Successfylly";
}
else
{
strSuccess = "Insertion Failed";
}
return strSuccess;
}
}
}
==================================

SQLHelper.cs:
*********************

using System;
using System.Data;
using System.Configuration;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;

public class SQLHelper
{
private string strConnectionString = string.Empty;

public SQLHelper()
{
// strConnectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
}

#region Common Function definition to execute any query

/// <summary>
/// Create the connection with the DataBase using web.config Appsettings tag.
/// </summary>
/// <returns> sqlconnection object</returns>
public SqlConnection connection()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString);
return con;
}

/// <summary>
/// Used to execute query using ExecuteNonQuery() Method
/// </summary>
/// <param name="Query"></param>
/// <returns>Boolean</returns>
public bool ExecuteNonQueryDB(string Query)
{
if (Query != "")
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(Query, m_Scon);
m_Scon.Close();
m_Scon.Open();
int m_Sdr = m_Scmd.ExecuteNonQuery();
if (m_Sdr > 0)
{
if (m_Scon.State == ConnectionState.Open)
{
m_Scon.Close();
}
return true;
}
else
{
if (m_Scon.State == ConnectionState.Open)
{
m_Scon.Close();
}
return false;
}
}
else
{
return false;
}
}

/// <summary>
/// Used to execute query using ExecuteReader() Method
/// </summary>
/// <param name="Query"></param>
/// <returns>IDataReader</returns>
public SqlDataReader ExecuteReaderDB(string Query)
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(Query, m_Scon);
m_Scon.Close();
m_Scon.Open();
SqlDataReader m_Sdr = m_Scmd.ExecuteReader(CommandBehavior.CloseConnection);
return m_Sdr;
}

/// <summary>
/// Used to execute query using ExecuteDataSet() Method
/// </summary>
/// <param name="Query"></param>
/// <returns>DataSet</returns>
public DataSet ExecuteDataSetDB(string Query)
{
if (Query != "")
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(Query, m_Scon);
DataSet m_DSObj = new DataSet();

SqlDataAdapter adr = new SqlDataAdapter(m_Scmd);
adr.Fill(m_DSObj);

if (m_Scon.State == ConnectionState.Open)
{
m_Scon.Close();
}

return m_DSObj;
}
else
{
return null;
}
}

///<summary>
///Used to execute the stored procedure and return DataSet
///</summary>
///<param name="ProcedureName"></param>
///<param name="Parameters"></param>
///<returns>DataSet</returns>
public DataSet ExecuteDataSetSPDB(string ProcedureName, SqlParameter[] Parameters)
{
if (ProcedureName != "")
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
m_Scmd.CommandType = CommandType.StoredProcedure;
DataSet m_DSObj = new DataSet();
if (Parameters != null)
{
foreach (SqlParameter para in Parameters)
{
m_Scmd.Parameters.Add(para);
}
}
SqlDataAdapter adr = new SqlDataAdapter(m_Scmd);
adr.Fill(m_DSObj);

if (m_Scon.State == ConnectionState.Open)
{
m_Scon.Close();
}

return m_DSObj;
}
else
{
return null;
}
}

/// <summary>
/// Used to execute the stored procedure and return IDataReader
/// </summary>
/// <param name="ProcedureName"></param>
/// <param name="Parameters"></param>
/// <returns>IDataReader</returns>
public SqlDataReader ExecuteReaderSPDB(string ProcedureName, SqlParameter[] Parameters)
{
if (ProcedureName != "")
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
m_Scmd.CommandType = CommandType.StoredProcedure;
m_Scon.Close();
m_Scon.Open();
if (Parameters != null)
{
foreach (SqlParameter para in Parameters)
{
m_Scmd.Parameters.Add(para);
}
}
SqlDataReader m_Sdr = m_Scmd.ExecuteReader(CommandBehavior.CloseConnection);


return m_Sdr;
}
else
{
return null;
}
}

/// <summary>
/// Used to execute the stored procedure using executenonquery
/// </summary>
/// <param name="ProcedureName"></param>
/// <param name="Parameters"></param>
/// <returns>Boolean</returns>
public bool ExecuteNonQuerySPDB(string ProcedureName, SqlParameter[] Parameters)
{
if (ProcedureName != "")
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
m_Scmd.CommandType = CommandType.StoredProcedure;
m_Scon.Close();
m_Scon.Open();
if (Parameters != null)
{
foreach (SqlParameter para in Parameters)
{
m_Scmd.Parameters.Add(para);
}
}
int rowsAffected = m_Scmd.ExecuteNonQuery();

if (rowsAffected > 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

/// <summary>
/// Used to execute stored procedure using executescalar method
/// </summary>
/// <param name="ProcedureName"></param>
/// <param name="Parameters"></param>
/// <returns>string</returns>
public string ExecuteScalarSPDB(string ProcedureName, SqlParameter[] Parameters)
{
if (ProcedureName != "")
{
SqlConnection m_Scon = connection();
SqlCommand m_Scmd = new SqlCommand(ProcedureName, m_Scon);
m_Scon.Close();
m_Scon.Open();
if (Parameters != null)
{
foreach (SqlParameter para in Parameters)
{
m_Scmd.Parameters.Add(para);
}
}
object rowsID = m_Scmd.ExecuteScalar();

return rowsID.ToString();
}
else
{
return "";
}
}

#endregion
}

Author: Suresh             
Posted Date: February 26, 2009     Points: 5   

hi Athira Appukuttan
can u plz send that sample code to my id. i want to run that in .net 2.0. this is my id urzsuresh(at)yahoo(dot)co(dot)in



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend