Click here to Skip to main content
15,920,603 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Questionhow to find substring count occurs in a string in asp.net Pin
$unil Dhiman17-Mar-08 21:52
$unil Dhiman17-Mar-08 21:52 
GeneralRe: how to find substring count occurs in a string in asp.net Pin
eyeseetee17-Mar-08 22:45
eyeseetee17-Mar-08 22:45 
GeneralRe: how to find substring count occurs in a string in asp.net Pin
$unil Dhiman18-Mar-08 0:52
$unil Dhiman18-Mar-08 0:52 
GeneralRe: how to find substring count occurs in a string in asp.net Pin
Christian Graus17-Mar-08 22:47
protectorChristian Graus17-Mar-08 22:47 
GeneralProblem in binding Menu with sitemap Pin
Deepak Nigam17-Mar-08 20:54
Deepak Nigam17-Mar-08 20:54 
GeneralRe: Problem in binding Menu with sitemap Pin
eyeseetee17-Mar-08 22:45
eyeseetee17-Mar-08 22:45 
GeneralRemember me Cookie Pin
nour12317-Mar-08 20:52
nour12317-Mar-08 20:52 
GeneralRe: Remember me Cookie Pin
sumit703417-Mar-08 21:21
sumit703417-Mar-08 21:21 
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: Remember me Cookie Pin
nour12317-Mar-08 22:39
nour12317-Mar-08 22:39 
GeneralRe: Remember me Cookie Pin
eyeseetee17-Mar-08 22:54
eyeseetee17-Mar-08 22:54 
GeneralRe: Remember me Cookie Pin
nour12318-Mar-08 6:49
nour12318-Mar-08 6:49 
Questiondisplay msexcel in panel Pin
iswaryaramkumar17-Mar-08 20:09
iswaryaramkumar17-Mar-08 20:09 
GeneralDouble post - please ignore Pin
pmarfleet17-Mar-08 20:50
pmarfleet17-Mar-08 20:50 
GeneralFTPweb request Pin
Laxmikant Lad17-Mar-08 19:46
Laxmikant Lad17-Mar-08 19:46 
Generalquery within query [modified] Pin
rahul.net1117-Mar-08 19:46
rahul.net1117-Mar-08 19:46 
Questiondisplay msexcel in panel Pin
iswaryaramkumar17-Mar-08 19:30
iswaryaramkumar17-Mar-08 19:30 
GeneralRe: display msexcel in panel Pin
Paul Conrad21-Mar-08 9:24
professionalPaul Conrad21-Mar-08 9:24 
General.dll file. Pin
nagendrathecoder17-Mar-08 19:28
nagendrathecoder17-Mar-08 19:28 
GeneralRe: .dll file. Pin
Christian Graus17-Mar-08 19:33
protectorChristian Graus17-Mar-08 19:33 
GeneralRe: .dll file. Pin
rahul.net1117-Mar-08 19:47
rahul.net1117-Mar-08 19:47 
Generalstyle="display:inline-block;border-width:1px;border-style:Solid;width:200px Pin
joshc17-Mar-08 18:53
joshc17-Mar-08 18:53 
GeneralRe: style="display:inline-block;border-width:1px;border-style:Solid;width:200px Pin
Christian Graus17-Mar-08 19:05
protectorChristian Graus17-Mar-08 19:05 
GeneralCustom Vallidation for file extension. Pin
SreejithAchutan17-Mar-08 18:48
SreejithAchutan17-Mar-08 18:48 
GeneralRe: Custom Vallidation for file extension. Pin
SreejithAchutan17-Mar-08 19:49
SreejithAchutan17-Mar-08 19:49 
GeneralRe: Custom Vallidation for file extension. Pin
saini arun18-Mar-08 11:19
saini arun18-Mar-08 11:19 

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.