How to associate some user data with the current HttpRequest?

I need to somehow bind my user data to the HttpRequest that is being processed by my custom IIS modules - so that code that runs in the early stages of the IIS pipeline attaches the object and code that runs in the later stages can retrieve the object and use its, and no other IIS pipeline processing functionality, is changed by adding this object.

Data should be stored in only one HTTP request - I do not need it to be stored between requests. I need it to be "reset" for each new request automatically, so when a new request arrives, it does not contain objects that my code is attached to the previous request.

HttpContext.Items seems to be the way to go, although the MSDN description of its purpose is not very clear.

HttpContext.Current.Items using the HttpContext.Current.Items way to solve my problem?

+6
source share
1 answer

This should work - I already did this in the project before.

I have a class that has a static property like this -

 public class AppManager { public static RequestObject RequestObject { get { if (HttpContext.Current.Items["RequestObject"] == null) { HttpContext.Current.Items["RequestObject"] = new RequestObject(); } return (RequestObject)HttpContext.Current.Items["RequestObject"]; } set { HttpContext.Current.Items["RequestObject"] = value; } } } 

And then RequestObject contains all my user data, so in my application I can do

 AppManager.RequestObject.CustomProperty 

So far, I have not encountered any problems in the way HttpContext.Items works.

+4
source

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


All Articles