i’m working on a custom login page in mvc.net. I check logins like so:
public bool Login(string login, string password, bool persistent)
{
var loginEntity = this.AdminRepository.GetLogin(login, password);
if (loginEntity != null)
{
FormsAuthentication.SetAuthCookie(login, persistent);
HttpContext.Current.Session["AdminId"] = loginEntity.AdminId;
HttpContext.Current.Session["AdminUsername"] = loginEntity.Username;
return true;
}
then i decorate any controller that needs admin access with a filter attribute:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ctx = HttpContext.Current;
// check if session is supported
if (ctx.Session != null)
{
var redirectTargetDictionary = new RouteValueDictionary();
// check if a new session id was generated
if (ctx.Session.IsNewSession)
{
// If it says it is a new session, but an existing cookie exists, then it must
// have timed out
string sessionCookie = ctx.Request.Headers["Cookie"];
if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
{
redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", "Admin");
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
} else if (SessionContext.AdminId == null) {
redirectTargetDictionary = new RouteValueDictionary();
redirectTargetDictionary.Add("area", "Admin");
redirectTargetDictionary.Add("action", "LogOn");
redirectTargetDictionary.Add("controller", "Home");
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
}
}
base.OnActionExecuting(filterContext);
}
I see that after log in I have two cookies:
- ASPXAUTH (with expiration date set
to “At end of session” (when
persists is false) OR (30 min from
now (when persists is set to true) - and ASP.NET_SessionId which
expiration time is always “At end of
session”
Question:
The problem is that even though i set TRUE to “persists” option (which will set ASPXAUTH expiration time 30 min from now -which is good) my Session[“AdminId”] is always null after i close and reopen the browser. How do i make sure my Sessions (Session[“AdminId”] and Session[“AdminUsername”]) are pulled in from the cookie when I initially do set “persists” to true and close then re-open the browswer window.
thanks
I found my solution here:Is it possible to use .ASPXAUTH for my own logging system?
and this is what i did: