Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Session

Session

Scheduled Pinned Locked Moved ASP.NET
question
15 Posts 6 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rock Star

    Hi, I want to know how can I have single active session for single user? For ex. Consider I login to website and after successful login my ID is stored in session and If I leave this session in active mode and login to the website using another PC I am able to login to the site and both of my session are active. How can I abandon previous session? Is there anyway I can achieve this? Thanking You!

    Rock Star

    M Offline
    M Offline
    m khansari
    wrote on last edited by
    #4

    Hi Why did not you deny that log-on when someone have logged on via that account already?

    Mohammad Khansari

    R 1 Reply Last reply
    0
    • M m khansari

      Hi Why did not you deny that log-on when someone have logged on via that account already?

      Mohammad Khansari

      R Offline
      R Offline
      Rock Star
      wrote on last edited by
      #5

      I dont want to hit my database for it. without hitting my database I want to achieve this functionality.

      Rock Star

      M 1 Reply Last reply
      0
      • R Rock Star

        Hi, I want to know how can I have single active session for single user? For ex. Consider I login to website and after successful login my ID is stored in session and If I leave this session in active mode and login to the website using another PC I am able to login to the site and both of my session are active. How can I abandon previous session? Is there anyway I can achieve this? Thanking You!

        Rock Star

        A Offline
        A Offline
        Abhishek Sur
        wrote on last edited by
        #6

        Just put the ip to storage while the user logs in. Also for every request, check if the client ip is there in the server. If the user logs in from another computer, just replace the ip with the new one. Thus any request from the other client will be rejected as it first checks to the database value which is changed. ;)

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        My Latest Articles-->** Simplify Code Using NDepend
        Basics of Bing Search API using .NET
        Microsoft Bing MAP using Javascript

        R 1 Reply Last reply
        0
        • A Abhishek Sur

          Just put the ip to storage while the user logs in. Also for every request, check if the client ip is there in the server. If the user logs in from another computer, just replace the ip with the new one. Thus any request from the other client will be rejected as it first checks to the database value which is changed. ;)

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          R Offline
          R Offline
          Rock Star
          wrote on last edited by
          #7

          That's a great idea But is there anything else so I dont have to hit the database.

          Rock Star

          A 1 Reply Last reply
          0
          • R Rock Star

            I dont want to hit my database for it. without hitting my database I want to achieve this functionality.

            Rock Star

            M Offline
            M Offline
            m khansari
            wrote on last edited by
            #8

            You can create a static class that contains SessionIDs and UserIDs(or IPAddresses) that logged on and use that for checking log-on. like static class SessionList { static ArrayList Sessions; ... } OR You can use Applications State for that

            Mohammad Khansari

            1 Reply Last reply
            0
            • R Rock Star

              Hi, I want to know how can I have single active session for single user? For ex. Consider I login to website and after successful login my ID is stored in session and If I leave this session in active mode and login to the website using another PC I am able to login to the site and both of my session are active. How can I abandon previous session? Is there anyway I can achieve this? Thanking You!

              Rock Star

              D Offline
              D Offline
              DJ Matthews
              wrote on last edited by
              #9

              You may want to try looking at the cache class. It's like a session, but it's application level(from what I've been reading about it) MSDN Link[]

              P 1 Reply Last reply
              0
              • R Rock Star

                That's a great idea But is there anything else so I dont have to hit the database.

                Rock Star

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #10

                You might use Cache or Application. Both are Global and In memory shared variable. In case of application you use : Application.Lock(); Application[currentUserName] = IP; Application.UnLock(); If you are using Cache, you dont need to lock as it handles automatically. ;)

                Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                My Latest Articles-->** Simplify Code Using NDepend
                Basics of Bing Search API using .NET
                Microsoft Bing MAP using Javascript

                1 Reply Last reply
                0
                • D DJ Matthews

                  You may want to try looking at the cache class. It's like a session, but it's application level(from what I've been reading about it) MSDN Link[]

                  P Offline
                  P Offline
                  Paulo Zemek
                  wrote on last edited by
                  #11

                  The solution I will give you will only work if you are NOT in a web-farm and if you have a Session manager that supports informing you when a session dies. Create a dictionary, like:

                  private static Dictionary<string, string> _someDictionaty = new Dictionary<string, string>();

                  Each time a user log-ins (after you validated the password) will do:

                  lock(_someDictionary)
                    _someDictionary[...loginHere...] = HttpContext.Current.Session.SessionID;

                  Except from the login page, you must check if the "actual" session is the active session for logged user. So:

                  string activeSessionId;
                  lock(_someDictionary)
                  {
                  if (_someDictionary.TryGetValue(...loginHere..., out activeSessionId)
                  {
                    // Some old session was found for this ID, so check if it is the same or not.
                    if (activeSessionId != HttpContext.Current.Session.SessionID)
                       // the activeSession is not this... so, show an error? Redirect to login?
                  }
                  else
                  {
                  // There is no session registered for this login. Is this an error?
                  }
                  }

                  And finally, in Global.asax, in the Session_End method, you must remove the session for that user, so:

                  var pair = new KeyValuePair<string, string>(...loginHere..., HttpContext.Current.Session.SessionID);
                  IDictionary<string, string> dictionary = _someDictionary;

                  lock(_someDictionary)
                  dictionary.Remove(pair);

                  I used the Remove that accepts a key/value pair because if a session ends that is not the valid one we must simple ignore it.

                  R 2 Replies Last reply
                  0
                  • P Paulo Zemek

                    The solution I will give you will only work if you are NOT in a web-farm and if you have a Session manager that supports informing you when a session dies. Create a dictionary, like:

                    private static Dictionary<string, string> _someDictionaty = new Dictionary<string, string>();

                    Each time a user log-ins (after you validated the password) will do:

                    lock(_someDictionary)
                      _someDictionary[...loginHere...] = HttpContext.Current.Session.SessionID;

                    Except from the login page, you must check if the "actual" session is the active session for logged user. So:

                    string activeSessionId;
                    lock(_someDictionary)
                    {
                    if (_someDictionary.TryGetValue(...loginHere..., out activeSessionId)
                    {
                      // Some old session was found for this ID, so check if it is the same or not.
                      if (activeSessionId != HttpContext.Current.Session.SessionID)
                         // the activeSession is not this... so, show an error? Redirect to login?
                    }
                    else
                    {
                    // There is no session registered for this login. Is this an error?
                    }
                    }

                    And finally, in Global.asax, in the Session_End method, you must remove the session for that user, so:

                    var pair = new KeyValuePair<string, string>(...loginHere..., HttpContext.Current.Session.SessionID);
                    IDictionary<string, string> dictionary = _someDictionary;

                    lock(_someDictionary)
                    dictionary.Remove(pair);

                    I used the Remove that accepts a key/value pair because if a session ends that is not the valid one we must simple ignore it.

                    R Offline
                    R Offline
                    Rock Star
                    wrote on last edited by
                    #12

                    Thanx Buddy

                    Rock Star

                    1 Reply Last reply
                    0
                    • P Paulo Zemek

                      The solution I will give you will only work if you are NOT in a web-farm and if you have a Session manager that supports informing you when a session dies. Create a dictionary, like:

                      private static Dictionary<string, string> _someDictionaty = new Dictionary<string, string>();

                      Each time a user log-ins (after you validated the password) will do:

                      lock(_someDictionary)
                        _someDictionary[...loginHere...] = HttpContext.Current.Session.SessionID;

                      Except from the login page, you must check if the "actual" session is the active session for logged user. So:

                      string activeSessionId;
                      lock(_someDictionary)
                      {
                      if (_someDictionary.TryGetValue(...loginHere..., out activeSessionId)
                      {
                        // Some old session was found for this ID, so check if it is the same or not.
                        if (activeSessionId != HttpContext.Current.Session.SessionID)
                           // the activeSession is not this... so, show an error? Redirect to login?
                      }
                      else
                      {
                      // There is no session registered for this login. Is this an error?
                      }
                      }

                      And finally, in Global.asax, in the Session_End method, you must remove the session for that user, so:

                      var pair = new KeyValuePair<string, string>(...loginHere..., HttpContext.Current.Session.SessionID);
                      IDictionary<string, string> dictionary = _someDictionary;

                      lock(_someDictionary)
                      dictionary.Remove(pair);

                      I used the Remove that accepts a key/value pair because if a session ends that is not the valid one we must simple ignore it.

                      R Offline
                      R Offline
                      Rock Star
                      wrote on last edited by
                      #13

                      Hi, I m getting the following errors. Error 1 The type or namespace name 'KeyValuePair' could not be found (are you missing a using directive or an assembly reference?) Error 2 The non-generic type 'System.Collections.IDictionary' cannot be used with type arguments Error 3 The name '_someDictionary' does not exist in the current context D:\Projects\Demo\Website\Global.asax Error 4 The name '_someDictionary' does not exist in the current context D:\Projects\Demo\Website\Global.asax And it is not allowing me to declare dictionary as private static

                      Rock Star

                      P 1 Reply Last reply
                      0
                      • R Rock Star

                        Hi, I m getting the following errors. Error 1 The type or namespace name 'KeyValuePair' could not be found (are you missing a using directive or an assembly reference?) Error 2 The non-generic type 'System.Collections.IDictionary' cannot be used with type arguments Error 3 The name '_someDictionary' does not exist in the current context D:\Projects\Demo\Website\Global.asax Error 4 The name '_someDictionary' does not exist in the current context D:\Projects\Demo\Website\Global.asax And it is not allowing me to declare dictionary as private static

                        Rock Star

                        P Offline
                        P Offline
                        Paulo Zemek
                        wrote on last edited by
                        #14

                        If I am not wrong, the KeyValuePair and the Dictionary classes are in the System.Collections.Generic But, to be honest, I always use the editor to add them to me. When you put the caret in the class name, a small rectangle appears. Click in it, and one of the options is to add the using reference.

                        R 1 Reply Last reply
                        0
                        • P Paulo Zemek

                          If I am not wrong, the KeyValuePair and the Dictionary classes are in the System.Collections.Generic But, to be honest, I always use the editor to add them to me. When you put the caret in the class name, a small rectangle appears. Click in it, and one of the options is to add the using reference.

                          R Offline
                          R Offline
                          Rock Star
                          wrote on last edited by
                          #15

                          Yes I am using system.collection.generic. But dont know why I am still getting the same error. Thanking You!

                          Rock Star

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups