开发者

.net forms authentication question

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 se开发者_开发百科e that after log in I have two cookies:

  1. 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)
  2. 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


A cookie with an expiry time gets written to disk. The user will therefore still be logged in next time they open a browser, provided that the cookie has not expired.

A session cookie is stored only in memory, and is lost as soon as the browser is closed.

A session cookie is a cookie without an expiry date.


I found my solution here:Is it possible to use .ASPXAUTH for my own logging system?

and this is what i did:

    public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    /// <summary>
    /// Controller action filter is used to check whether the session is still active. If the session has expired filter redirects to the login screen.
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var ctx = HttpContext.Current;

        // check if session is supported
        if (ctx.Session != null)
        {
            // check if a new session id was generated
            if (ctx.Session.IsNewSession)
            {
                var identity = ctx.User.Identity;

                // 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)
                {
                    var redirectTargetDictionary = new RouteValueDictionary();
                    redirectTargetDictionary.Add("area", string.Empty);
                    redirectTargetDictionary.Add("action", "LogOn");
                    redirectTargetDictionary.Add("controller", "User");

                    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
                }

                // Authenticated user, load session info
                else if (identity.IsAuthenticated)
                {
                    var loginRepository = new LoginRepository(InversionOfControl.Container.Resolve<IDbContext>());
                    IAuthenticationService authenticationService = new AuthenticationService(loginRepository);
                    authenticationService.SetLoginSession(identity.Name);
                }
            }
            else if (SessionContext.LoginId == null)
            {
                var redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("area", string.Empty);
                redirectTargetDictionary.Add("action", "LogOn");
                redirectTargetDictionary.Add("controller", "User");

                filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }
        }
        base.OnActionExecuting(filterContext);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜