ASP MVC changes the cookie value of a user who does not send a request

It is assumed that we have two users:    user 1,user 2

I want to user 1send a request to change the cookie value, the cookie value user 2has changed.

Here is my code in the class:

public static void changeCookieCount(Int64 targetUserId)
    {
        int a = //get cookie value from database via `targetUserId`
        HttpCookie cookieCount = HttpApp.RequestCookie("cookieCount");

        if (cookieCount != null)
        {
            cookieCount.Value = a;
            cookieCount.Expires = DateTime.Now.AddSeconds(5);
            cookieCount.Expires = DateTime.Now.AddMonths(2);
            cookieCount.Secure = true;
            HttpApp.ResponseCookie(cookieCount);
        }
        else
        {
            cookieCount = new HttpCookie("cookieCount");
            cookieCount.Value = a;
            cookieCount.Expires = DateTime.Now.AddSeconds(5);
            cookieCount.Expires = DateTime.Now.AddMonths(2);
            cookieCount.Secure = true;
            HttpApp.ResponseCookie(cookieCount);
        }
    }

As you can see, I am changing the value of the cookie in the class with the user HttpApp, and this is part of the "HttpApp":

 public class HttpApp: HttpApplication
{
    public static void ResponseCookie(HttpCookie cookie)
    {
        HttpContext.Current.Response.Cookies.Add(cookie);
    }
    public static HttpCookie RequestCookie(string name)
    {
        return HttpContext.Current.Request.Cookies[name];
    }
}

My question is: how to access the target user to change the cookie value?

+4
source share
1 answer

Save the request to change the cookie user_1 in the database. When user_2 returns, check db and write a new cookie value for user_2.

, userId cookie . user_2 , user_1 .

ActionFilterAttribute, :

public class EachRequestFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Session == null)
            return;

        if (filterContext.HttpContext.Session["UserId"] == null)
            return;

         // Check database for any cookie change for this UserId and if there is a change update cookie value...

    }
}

, !

-1

All Articles