Base solution for your next web application
Open Closed

session with asp.net session #282


User avatar
0
jesse created

Hi

I Use HttpContext.Current.Session int my project ,when I login succeed,I Assign values to the AbpSession object's properties , but when I refresh my page, abpSession object's properties always is null . but sometimes it's ok .how can I Keep Session State怂Do I need to Assign values in Application_BeginRequest(AbpWebApplication ) method ?

thank you


3 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    IAbpSession's setters are readonly. How do you assign? Have you implemented IAbpSession for your own project? If so, no problem. In this implementation, you can get from Session.

    Are you creating your actions/methods async? If so, HttpContext.Current may not be reached in async code sometimes.

    If you set AbpSession class properties;

    • if your session class is transient, it's created per injection and setting it meaningless since when you inject it you will get a new instance and UserId will be null.
    • if it's singleton, it's shared by all concurrent requests !! So, it should not be singleton.

    You can get from ASP.NET's Session by implementing IAbpSession. That's safer.

  • User Avatar
    0
    jesse created

    Thank you for your answer,I have modified my code.

    public int? UserId
            {
                get
                {
                    var userId = HttpContext.Current.Session["UserId"];
                    if (userId == null)
                    {
                        return null;
                    }
                    return Convert.ToInt32(userId);
                }
                set { throw new NotImplementedException(); }
            }
    

    but I'm worried about high concurrency and Multithreading, session will have problem,And mobile App can use like this?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    AspNet's Session will not be problem in multi-user environment. But this is AspNet's issue, not related to ABP :)

    I don't suggest to use Session ! Instead, get UserId from cookie or a token (for token based auth for mobile apps).