1

I'm starting out with ASP.NET MVC (4).

Throughout the app, I often depend on an employee key. I can get this key from the database via the current user's username which is accessible to me via User.Identity.Name.

Rather than repeat this look-up throughout the controllers, what is the correct way to do the look-up once and store it in a variable which can then be accessed across all controllers?

Isaac Kleinman
  • 3,994
  • 3
  • 31
  • 35
  • 2
    Sounds like the principle is wrong. For what purpose do you need the employee key? why don't you save it on a cookie? if you don't want to expose it, you can put it on a session variable... – ilans Oct 23 '14 at 18:30
  • 1
    Look at this: http://stackoverflow.com/questions/8891472/store-session-info-in-asp-net-cookie-or-session-state – ilans Oct 23 '14 at 18:33
  • How do I save it on a cookie? – Isaac Kleinman Oct 23 '14 at 18:55
  • See my answer down here – ilans Oct 23 '14 at 18:58
  • 1
    Why not just do that look up? Sending as a cookie means you send it back and forth to the client on every request. Is that really worth the nanoseconds it takes to do that look up? – Heretic Monkey Oct 23 '14 at 19:19
  • @MikeMcCaughan - It's just a key... reading from a DB is much more expensive. – ilans Oct 23 '14 at 19:21
  • @ilanS, The code `User.Identity.Name` does not indicate a database look up to me, and the OP said nothing about a database. In fact, it seems to use the `User` property of the built-in `Controller` class, which would be set during authentication, and therefore would be available for all authenticated actions, in any controller, without sending anything to the client. – Heretic Monkey Oct 23 '14 at 19:28
  • @MikeMcCaughan: I use `User.Identity.Name` to look up the employee ID in the database. – Isaac Kleinman Oct 23 '14 at 19:31
  • 1
    Guys - I understood exactly what @IsaacKleinman just said... that he does look up the database - using what he receives from the `User` class. That was an assumption. – ilans Oct 23 '14 at 19:32
  • Well, perhaps for the less psychic of those of us in the audience, that could be included in the question itself? That would make it a better question. As it is, this seems like a duplicate of several questions, including this one: http://stackoverflow.com/questions/5118610/asp-net-mvc-global-variables – Heretic Monkey Oct 23 '14 at 19:36
  • Yeah, but this question also refers to the controllers of MVC. – ilans Oct 23 '14 at 19:39

1 Answers1

2

You can save it on a cookie this way:

Dim cookie As New HttpCookie("SomeNameForCookie")

And put variables on this cookie this way:

cookie("Name1") = "Some Value Can be also integer etc"
cookie("Name2") = 2

To get the cookie you do:

HttpContext.Current.Request.Cookies.Get("SomeNameForCookie")

And you read the values from the cookie the same way as you put values (cookie("Name1") etc).


You can also save session variables this way:

Session("Name3") = 3

A third way would be to create a controller base class. Inherit all/some of your controllers from it. When it gets filled with some value (protected or public) it would be available throughout all your controllers.

ilans
  • 2,537
  • 1
  • 28
  • 29
  • Is use of session variables in line with the MVC way of doing things? – Isaac Kleinman Oct 23 '14 at 19:06
  • Yes of course. But remember that it costs memory and it's usually saved for a period of time (20 minutes is the default). Read a little about it if you gonna store a lot of info into this. Try to be as stateless as you can.. this is the best practice. But for specific needs `Session` can be good. In your case it sounds a cookie is the best practice. – ilans Oct 23 '14 at 19:08
  • So would doing a database look-up be more recommended? – Isaac Kleinman Oct 23 '14 at 19:12
  • Look, if this data is needed in lots of places and it's not a security issue - i'd use a cookie. I save a customer ID in a cookie myself. If you don't wanna expose this info - you can fetch it from the DB. You can also encrypt it to a cookie btw... but that's an overkill for you. You also can make a base controller and inherit from it - look at my answer third option. – ilans Oct 23 '14 at 19:17
  • 1
    My recommendation - just choose your way and go along with no fear ! :) what will happen?? – ilans Oct 23 '14 at 19:20
  • 1
    @IsaacKleinman - So, do you accept my answer? did it help you? :) – ilans Oct 23 '14 at 19:35
  • Of course you've helped! Should I wait a bit to give someone else a chance as well? – Isaac Kleinman Oct 23 '14 at 19:37
  • Yeah.. that would be interesting. – ilans Oct 23 '14 at 19:38