locked
Current user Session in MVC RRS feed

  • Question

  • User-515183 posted

    What is the best way to retain the user.identity.Name for the current user along with email , employee number etc when users log in which will be many users obtaining the same form. 

    Currently putting the ling to sql results in Session['name"] = results.FullName

    and so on ...when the post is done for this user set Session.Abandon();

    Sunday, May 21, 2017 11:40 PM

Answers

  • User-271186128 posted

    Hi subggle,

    According to your description, you store a string in the session, and also you could store an object in the session, look at the following code.

    (1) create the class of user model:

    public class User
    {
        public string Id
        {
            get; set;
        }
    
        public string FullName
        {
            get; set;
        }
    
        public string Email
        {
            get; set;
        }
    
        public string EmployeeNumber
        {
            get; set;
        }
    }
    

    (2)After login succeed, you could create an instance of the user model, then store it in the session.

        public void AfterLoginSucceed()
            {
                var user = new User
                {
                    Email = "tom@example.com",
                    EmployeeNumber = "0000001",
                    FullName = "Tom James",
                    Id = "1"
                };
                Session.Add("CurrentUser", user);
            }
    

    (3)You could get current user from the session such like this.

    User currentUser = (User)Session["CurrentUser"];

    sbuggle

    when the post is done for this user set Session.Abandon();

    If you doesn't call Session.Abandon() explicitly, it will be called automatically when session time out.
    Session.Abandon() means that destroy all the values in the session and release their resources, and then begin a new session.
    you could refer to the following link.
    https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon(v=vs.110).aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 22, 2017 5:16 AM
  • User-515183 posted

    Thank you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 22, 2017 11:51 AM

All replies

  • User-271186128 posted

    Hi subggle,

    According to your description, you store a string in the session, and also you could store an object in the session, look at the following code.

    (1) create the class of user model:

    public class User
    {
        public string Id
        {
            get; set;
        }
    
        public string FullName
        {
            get; set;
        }
    
        public string Email
        {
            get; set;
        }
    
        public string EmployeeNumber
        {
            get; set;
        }
    }
    

    (2)After login succeed, you could create an instance of the user model, then store it in the session.

        public void AfterLoginSucceed()
            {
                var user = new User
                {
                    Email = "tom@example.com",
                    EmployeeNumber = "0000001",
                    FullName = "Tom James",
                    Id = "1"
                };
                Session.Add("CurrentUser", user);
            }
    

    (3)You could get current user from the session such like this.

    User currentUser = (User)Session["CurrentUser"];

    sbuggle

    when the post is done for this user set Session.Abandon();

    If you doesn't call Session.Abandon() explicitly, it will be called automatically when session time out.
    Session.Abandon() means that destroy all the values in the session and release their resources, and then begin a new session.
    you could refer to the following link.
    https://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.abandon(v=vs.110).aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 22, 2017 5:16 AM
  • User-515183 posted

    Thank you

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 22, 2017 11:51 AM