ASP.NET MVC - an alternative to using a session

I have an ASP.NET MVC view that uses jquery.Uploadify to send files to one of my controllers for use in my application and one of the side effects that I noticed with the Download program is that when I download Flash Upload file to send files to server messages to my controller, it gets its own SessionID from ASP.NET. Of course, this would be normal if my download controller did not use a session to store a list of files that were downloaded by the current user for future manipulations using my application ... Therefore, given this problem, after downloading files with my view, the current user session does not contain any of the files that have just been published.

Any suggestions on how to achieve what I want without relying on sessions (and preferably without a database)?

+3
source share
4 answers

This is the solution I came across. I have not done much testing, but it seems to be an acceptable alternative to the session in my current scenario. I will use Global.asax Session_End / Session_Start to ensure that rows are created and deleted as needed.

public class UserTable : Dictionary<string, Dictionary<string, object>>
{
    public new object this[string key]
    {
        get
        {
            object value = null;
            if (HttpContext.Current != null)
            {
                var sessionId = HttpContext.Current.Session.SessionID;
                if (ContainsKey(sessionId) && base[sessionId].ContainsKey(key))
                    value = base[sessionId][key];
            }
            else
                throw new Exception("No HttpContext present.");
            return value;
        }
        set
        {
            if (HttpContext.Current != null)
            {
                var sessionId = HttpContext.Current.Session.SessionID;
                if (!ContainsKey(sessionId))
                    Add(sessionId, new Dictionary<string, object>());
                if (!base[sessionId].ContainsKey(key))
                    base[sessionId].Add(key, value);
                else
                    base[sessionId][key] = value;
            }
            else
                throw new Exception("No HttpContext present.");
        }
    }

    public object this[string sessionId, string key]
    {
        get
        {
            object value = null;
            if (ContainsKey(sessionId) && base[sessionId].ContainsKey(key))
                value = base[sessionId][key];
            return value;
        }
        set
        {
            if (!ContainsKey(sessionId))
                Add(sessionId, new Dictionary<string, object>());
            if (!base[sessionId].ContainsKey(key))
                base[sessionId].Add(key, value);
            else
                base[sessionId][key] = value;
        }
    }

    public void Add(string sessionId)
    {
        Add(sessionId, new Dictionary<string, object>());
    }

    public void Add()
    {
        if (HttpContext.Current != null)
            Add(HttpContext.Current.Session.SessionID);
        else
            throw new Exception("No HttpContext present.");
    }

    public new void Remove(string sessionId)
    {
        base.Remove(sessionId);
    }

    public void Remove()
    {
        if (HttpContext.Current != null)
            Remove(HttpContext.Current.Session.SessionID);
        else
            throw new Exception("No HttpContext present.");
    }
}
0
source

Since Uploadify is a pure script interface, I do not understand why it will receive a session from ASP.NET. I also do not quite understand what your specific problem is.

, , , , Uploadify. , , , .

+2

, , : ip ? , .

0

- "", , ? ASP.NET , .

0

Source: https://habr.com/ru/post/1714903/


All Articles