Frage

ich arbeite auf einer benutzerdefinierten Login-Seite in mvc.net. Ich überprüfe Anmeldungen wie folgt:

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;
  }

dann dekorieren i jeden Controller, der Admin-Zugriff mit einem Filter-Attribut muss:

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);
}

Ich sehe, dass nach der Anmeldung Ich habe zwei Cookies:

  1. ASPXAUTH (mit Ablaufdatum zu „Am Ende der Sitzung“ (wenn verharrt ist false) OR (30 min von jetzt (wenn verharrt auf true gesetzt ist)
  2. und ASP.NET_SessionId die Ablaufzeit ist immer „Am Ende der session "

Frage: Das Problem ist, dass, obwohl ich auf „verharren“ Option TRUE gesetzt (die ASPXAUTH Ablaufzeit 30 min ab sofort eingestellt wird -Welche ist gut) meine Session [ „AdminId“] ist immer null, nachdem ich schließen und den Browser wieder öffnen. Wie kann ich sicherstellen, dass meine Sessions (Session [ „AdminId“] und Session [ „Admin“]) gezogen in aus dem Cookie, wenn ich anfänglich noch „verharrt“ auf true und schließen Sie dann die browswer Fenster wieder öffnen. Dank

War es hilfreich?

Lösung 2

fand ich meine Lösung hier: Ist es möglich .ASPXAUTH für mein eigenes Logging-System zu benutzen?

und das ist, was ich tat:

    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);
    }
}

Andere Tipps

Ein Cookie mit einer Ablaufzeit wird auf die Platte geschrieben. Der Benutzer wird daher nach wie vor in der nächsten Zeit, die sie einen Browser öffnen angemeldet sein, vorausgesetzt, dass das Cookie nicht abgelaufen ist.

Ein Session-Cookie wird nur im Speicher abgelegt und ist verloren, sobald der Browser geschlossen wird.

Ein Session-Cookie ist ein Cookie ohne Ablaufdatum.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top