I have a User table. I’m using Forms authentication and have the following code for logon:
public JsonResult LogOn(FormCollection form)
{
var agent = SiteUserRepository.CheckAgent(form["Email"], form["Password"]);
if (agent == null)
return Json(new
{
IsSuccess = false,
Message = "Wrong email or password"
});
SiteUserRepository.UpdateLastLogon(agent.Email);
FormsAuthentication.SetAuthCookie(agent.Email, true);
ApplicationSession.CurrentUser = agent;
return Json(new
{
IsSuccess = true
});
}
The ApplicationSession is my wrapper for Session object.
public static class ApplicationSession
{
private const string _currentUser = "CurrentUser";
public static SiteUser CurrentUser
{
get
{
var user = HttpContext.Current.Session[_currentUser];
if (user != null)
return (SiteUser)user;
return null;
}
set
{
HttpContext.Current.Session[_currentUser] = value;
}
}
}
The session timeout equals 1440 (24 h). I need exactly this value.
For example user login on site. Then I delete this user from DB. And user will be authenticated (if they don’t click Log Off, of course). What is the best approach to solve this?
You want to use a cookie. Your code needs to check for the cookie and if it doesn’t exist, or the time has expired on it, you create a new one.
I think this will show you a nice example:
C# Cookies based on login information
http://www.beansoftware.com/ASP.NET-Tutorials/Cookies-ASP.NET.aspx
a remember me checkbox type implementation with an expiration of a day could be done something like this: