Click here to Skip to main content
15,890,506 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: GirdView with Image in Header, Export to Excel Pin
puviyarasan.j20-Oct-08 3:19
puviyarasan.j20-Oct-08 3:19 
GeneralRe: GirdView with Image in Header, Export to Excel Pin
saini arun20-Oct-08 3:43
saini arun20-Oct-08 3:43 
GeneralRe: GirdView with Image in Header, Export to Excel Pin
saini arun26-Oct-08 20:44
saini arun26-Oct-08 20:44 
QuestionHow to deal with Remeber me Option during login to site Pin
Rameez Raja18-Oct-08 21:36
Rameez Raja18-Oct-08 21:36 
AnswerRe: How to deal with Remeber me Option during login to site Pin
elektrowolf18-Oct-08 22:49
elektrowolf18-Oct-08 22:49 
AnswerRe: How to deal with Remeber me Option during login to site Pin
Abhijit Jana19-Oct-08 2:38
professionalAbhijit Jana19-Oct-08 2:38 
GeneralRe: How to deal with Remeber me Option during login to site Pin
Rameez Raja19-Oct-08 17:15
Rameez Raja19-Oct-08 17:15 
GeneralRe: How to deal with Remeber me Option during login to site Pin
sumit703419-Oct-08 18:09
sumit703419-Oct-08 18:09 
This article shows you how to implement the above functionality in ASP.NET.



Step 1: Add a checkbox in the login page
You can add a checkbox in the login page by dragging and dropping a checkbox from the toolbox when you are in page design mode, or you can create a checkbox programmatically in the code behind class.

The following code shows you how to do this in the second way:



private CheckBox _rememberMeCheckBox;

//.....

this._rememberMeCheckBox = new CheckBox();
this._rememberMeCheckBox.Text = "Remember me next time");
this._rememberMeCheckBox.Checked = true;
this.Controls.Add(this._rememberMeCheckBox);

//.....



Setp 2: Creat a cookie when a subscriber login
create a cookie in the Cliked event handler of the login button:

private void _LoginButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//....
//....

ApplicationUser user = ApplicationUser.RetrieveCurrentUserByLoginId(userId);

if (user != null && user.Password == password)
{
Session["user"] = user;

Response.Cookies.Remove("CookieFromXXX");

if (this._rememberMeCheckBox != null &&

this._rememberMeCheckBox.Checked)
{
HttpCookie cookie = new HttpCookie(string.Format("cookie_from_{0}", this._host));

cookie.Values.Add("userId", user.Id);
cookie.Values.Add("pwd", user.Password);
cookie.Expires = DateTime.Now.AddYears(1);

Response.Cookies.Add(cookie);
}
}

//....
//....
}



Step 3: Override the OnInit method in the base page
If there is a base page which is the parent page of all other pages in your website, you can override the the OnInit method in the base page like the following code:

override protected void OnInit(EventArgs e)
{

//....
//....

if (!IsPostBack && Session["user"] == null)
{
if (Request.Cookies["CookieFromXXX"] != null)
{
HttpCookie cookie = Request.Cookies["CookieFromXXX"];

string userId = cookie.Values["userId"].ToString();
string pwd = cookie.Values["pwd"].ToString();

ApplicationUser user = ApplicationUser.RetrieveCurrentUserByLoginId(userId);

if (user != null user.Password == pwd)
{
Session["user"] = user;
}
}
}

base.OnInit(e);
}

Or you can add the above logic in the OnLoad method in the home page.



Step 4: Use the Session["user"] to determine the subscriber logged in or not
In any web page, you can check the Session["user"] to determine the subscriber logged in or not, see the following code:



if (Session["user"] != null)
{
//logged in already
//....
}
else
{
//haven't logged in yet
//....
}



Step 5: Expire the cookie when subscriber logout the website
Add the following code to expire the cookie when subscribers logout the website.



this.Session.Clear();

if (Response.Cookies["CookieFromXXX{0}"] != null)
{
//Response.Cookies.Remove("CookieFromXXX");
Response.Cookies["CookieFromXXX"].Expires = DateTime.Now.AddDays(-1);
}

Response.Redirect("./homepage.aspx");
GeneralRe: How to deal with Remeber me Option during login to site Pin
Abhijit Jana19-Oct-08 20:01
professionalAbhijit Jana19-Oct-08 20:01 
GeneralThanks Pin
sumit703419-Oct-08 22:03
sumit703419-Oct-08 22:03 
AnswerRe: How to deal with Remeber me Option during login to site Pin
N a v a n e e t h19-Oct-08 18:32
N a v a n e e t h19-Oct-08 18:32 
QuestionTabcontainer: ".aspx" page for each tab (like ie or firefox), is this impossible? Pin
BlackDice18-Oct-08 8:12
BlackDice18-Oct-08 8:12 
QuestionHow can I cast an httpcontext.current.session object to a List of class? Pin
JUNEYT18-Oct-08 5:01
JUNEYT18-Oct-08 5:01 
AnswerRe: How can I cast an httpcontext.current.session object to a List of class? Pin
Guffa19-Oct-08 15:47
Guffa19-Oct-08 15:47 
GeneralRe: How can I cast an httpcontext.current.session object to a List of class? Pin
JUNEYT19-Oct-08 22:41
JUNEYT19-Oct-08 22:41 
GeneralRe: How can I cast an httpcontext.current.session object to a List of class? Pin
Guffa20-Oct-08 0:57
Guffa20-Oct-08 0:57 
GeneralRe: How can I cast an httpcontext.current.session object to a List of class? Pin
JUNEYT20-Oct-08 1:08
JUNEYT20-Oct-08 1:08 
GeneralRe: How can I cast an httpcontext.current.session object to a List of class? Pin
Guffa20-Oct-08 3:15
Guffa20-Oct-08 3:15 
QuestionDrop-Down Filter List for a DataGridView Column Pin
nandhububbly18-Oct-08 3:52
nandhububbly18-Oct-08 3:52 
AnswerRe: Drop-Down Filter List for a DataGridView Column Pin
Sathesh Sakthivel18-Oct-08 4:09
Sathesh Sakthivel18-Oct-08 4:09 
QuestionFILLING 2 TEXTBOX at same time ??? Pin
Karan_TN18-Oct-08 1:39
Karan_TN18-Oct-08 1:39 
Questionusing skins of your own made Pin
Alok Sharma ji18-Oct-08 1:21
Alok Sharma ji18-Oct-08 1:21 
AnswerRe: using skins of your own made Pin
Abhijit Jana18-Oct-08 4:09
professionalAbhijit Jana18-Oct-08 4:09 
Questionjavascript function to check value present in gridview or not Pin
Learning IT18-Oct-08 1:07
Learning IT18-Oct-08 1:07 
Questionjavacsripr problem of text box Pin
raghvendrapanda18-Oct-08 0:35
raghvendrapanda18-Oct-08 0:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.