site wide variables per user

前端 未结 1 1051
名媛妹妹
名媛妹妹 2021-01-16 06:42

I have an MVC4 site that needs to maintain some information while (and ONLY while) the user is logged in. For example, once the user logs in, I get a \'user token\' back tha

相关标签:
1条回答
  • 2021-01-16 07:27

    You want to use Session in ASP.net. It was created for the purpose you describe.

    ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session. By default, ASP.NET session state is enabled for all ASP.NET applications.

    I'm a fan of strongly-typed reusable session variables, so I wrote the following extensions to store whatever variables you want to create without the need to constantly remember magic strings.

    public static class SessionExtensions
    {
        public static bool TryGetValue<T>(this HttpSessionStateBase session, out T value) 
          where T : class
        {
            var name = typeof(T).FullName;
    
            value = session[name] as T;
    
            var result = value != null;
    
            return result;
        }
    
        public static void SetValue<T>(this HttpSessionStateBase session, T value)
        {
            var name = typeof(T).FullName;
    
            session[name] = value;
        }
    
        public static void RemoveValue<T>(this HttpSessionStateBase session)
        {
            var name = typeof(T).FullName;
    
            session[name] = null;
        }
    
        public static bool ValueExists(this HttpSessionStateBase session, Type objectType)
        {
            var name = objectType.FullName;
    
            var result = session[name] != null;
    
            return result;
        }
    }
    

    So if you have a class:

    public MyClass
    {
      public int MyInt { get; set; }
    }
    

    You can store it by simply:

    Session.SetValue(MyClass);
    

    that needs to maintain some information while (and ONLY while) the user is logged in.

    These methods could be updated a few ways to fulfill this requirement. Here is one way:

    public static bool TryGetAuthenticatedValue<T>(this HttpSessionStateBase session, 
      out T value) 
      where T : class
    {
        value = null;
    
        if (HttpContext.Current.User != null
            && HttpContext.Current.User.Identity != null
            && HttpContext.Current.User.IsAuthenticated)
        {
          var name = typeof(T).FullName;
    
          value = session[name] as T;
        }
    
        var result = value != null;
    
        return result;
    }       
    

    I would also recommend that whatever classes you store in session, be serializable. That is to say it has a parameterless constructor and marked as [Serializable].

    0 讨论(0)
提交回复
热议问题