views:

297

answers:

5

HI

I am using asp.net mvc with asp.net membership.

I want to have a checkbox that if clicked keeps the users signed in for 2 weeks(unless they clear their cookies).

So I know their is

FormsAuthentication.SetAuthCookie(userName, createPersistentCookie)

but I don't know how to set it up for 2week retention.

I rewrote most of the membership stuff. So I don't use stuff like Create() and VerifyUser().

+6  A: 

Add a secret hash key of a random string to both the cookie and the database (both the same key). If the cookie and database value are the same, when the user starts a new session, sign him/her in again. When the user reaches the two weeks, remove the secret key from the database using a cronjob (Unix) or scheduled task (Windows).

Warning: Do not rely on the cookie expire date, since people can hack their browser.

Time Machine
+2  A: 

Just use a simple session cookie with 2 weeks expiration date.

Traveling Tech Guy
By far the simplest way - but also the easiest to hack.
Daniel May
+1  A: 

Have you seen this?

http://forums.asp.net/t/1440824.aspx

Along similar lines to what Koning has suggested.

griegs
+3  A: 

You can set the global session timeout (the value is in minutes) in web.config eg.

<system.web>
    <authentication mode="Forms">
          <forms timeout="20160"/>
    </authentication>
</system.web>

This will be for all authenticated users. If you want to use the 'Remember Me' functionality then you will need to write your own code to set the cookie/ticket. Something like this (taken from here):

protected void Page_Load()
{
 if (Request.Cookies["username"] == null || Request.Cookies["username"].Value.ToString().Trim() == "")
{
Login1.RememberMeSet = true; 
}
else
{
Login1.UserName = Request.Cookies["username"].Value.ToString().Trim();
Login1.RememberMeSet = true; 
}
}
protected void RememberUserLogin()
{
// Check the remember option for login

if (Login1.RememberMeSet == true)
{
HttpCookie cookie = new HttpCookie("username");
cookie.Value = Login1.UserName.Trim(); 
cookie.Expires = DateTime.Now.AddHours(2);

HttpContext.Current.Response.AppendCookie(cookie);
Login1.RememberMeSet = true; 

}
else if (Login1.RememberMeSet == false)
{
HttpContext.Current.Response.Cookies.Remove("username");
Response.Cookies["username"].Expires = DateTime.Now;
Login1.RememberMeSet = false; 
}

}
Dan Diplo
but how about if they don't check the box? won't everyone get that setting?
chobo2
Yes, sorry, I misread your question - I will update my response to address your specific question...
Dan Diplo
I think if I just set the stuff like you have in the web.config but disable sliding expiry then I will get what I want.
chobo2
A: 

Instead of writing new stuff, how about implementing OpenID?

Simon Svensson