2

I need to be able to easily grab a username for a lot of queries we're running in an assembly outside the MVC project. I would just add a parameter for all the calls, but there are literally hundreds of them.

My colleague has advised me to pass a static reference to the HttpContext (presumably in OnApplicationStarted or something like that) and get the username from there.

HttpContext always has information pertaining to the current user's session. My concern with this is that if my object (that exists in a separate assembly) is not going to know which user exactly to pull the information from, and that while this may work for a single user, it will pull the wrong username as more users start accessing the system.

Is this a viable way of identifying the username?

How does HttpContext know which user is the current user? Is that mechanism severed outside the project?

micahhoover
  • 2,101
  • 8
  • 33
  • 53
  • if you can use Session in outside library then you should put username in session and access it from there. All mean to say fetching username info from session. – Ankush Jain Apr 22 '16 at 12:49
  • @AnkushJain: Interesting to note you think it should work. My fear is that since this would be a static object (i.e. only 1 for all users) when the username is pulled it might be for someone else. – micahhoover Apr 22 '16 at 12:55
  • I am not saying to create a static variable. I am asking to use session variables. And sessions are always user specific. – Ankush Jain Apr 22 '16 at 12:57
  • This might break your illusion that session(s) are shared across all users http://stackoverflow.com/questions/7298974/how-the-session-work-in-asp-net#answer-7298986 – Ankush Jain Apr 22 '16 at 13:01
  • @AnkushJain. Thanks for clarifying. So if I pass a session variable to an outside project and the object in that project periodically checks it, will it always point to the value for the current user? – micahhoover Apr 22 '16 at 13:05
  • 1
    I don't think you are clear of what you are wanting to do (or not explaining it well). Do you need a username for the user who is making the current request? or do you need a list of all users in your application? Certainly passing `HttpContext` around is not a solution, but `User` property is available from `HttpContext` and can be passed around as `IPrinciple` – trailmax Apr 22 '16 at 13:19
  • 2
    I would not pass `Session` to a library. If it needs a user name, just pass it a user name. If the library gets the user name from an instance of `HttpSessionState` and you change how you store your values in `Session` then you'll need to update the library. Passing unnecessary implementation details from one class to another (regardless of whether they're in different libraries) entangles them so that you can't change one without changing the other. – Scott Hannen Apr 22 '16 at 13:20
  • 1
    Look..You use session variable, pass it's value to a method which you have defined in separate library. that library will use that value and execute the code. There is no sense of checking value periodically. – Ankush Jain Apr 22 '16 at 13:21

2 Answers2

2

An HttpContext instance isn't global - it is the context for a specific request. So, for example, if you're executing a controller action and you call HttpContext (that's Controller.HttpContext) then you're dealing with the context for that request. So there's no question of which user the context is for - it's for the user that initiated that request.

You wouldn't want to reference it from OnApplicationStarted. It either won't be available or it will be the context for the current request.

That being said, if what you need is a user name then I'm not sure why you'd want to pass a whole HttpContext. A function should only receive the parameters that it needs. If it needs a user name you can get it from HttpContext.User.Identity.Name. That will make the other library easier to test. And what if the other library is called but not from a web application? Then there won't be an HttpContext to pass to it.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
0

Not sure if others were suggesting this very thing, but here goes ...

My colleague advised me to reference System.Web like so:

var username = System.Web.HttpContext.Current.Session["user"];

I get back a username.

I'm not totally sure this is a concurrently reliable solution, but there doesn't seem to be a handy way to test this (without an elaborate load test).

If two worker threads call this library at the same time, I'm not sure how the .NET framework knows the external library wants the one username from one place and the other from the other place.

micahhoover
  • 2,101
  • 8
  • 33
  • 53