views:

111

answers:

3

When a user logs on to my ASP.NET MVC application I need to persist what company that user belongs to. The company they belong to will determine what database ~all~ their queries come from so it is important for me to query for their company as soon as they log in and persist it so I don't have to perform this lookup again.

Where/How should I store this Company ID? Session? Some way to customize a MembershipProvider that would allow me to retain this information in a User object?

What would be the best way to handle this? Literally, every query I do after that will wildly depend on that company ID so it is important that I retain that information.

+1  A: 

You could use ASP.NET's Profile Properties, but you need to create your own handler for them. Check out Seventh Element's answer to this question for ideas on how to get started.

Matthew Jones
+1  A: 

You could write your own Auth module to extend FormsAuthentication and store the company in the IPrincipal.

Then you get to do User.Identity.Company just like User.Identity.Name.

This is more up-front work than sticking the company in an already-existing bucket like Cache or Session, but it lets you write some pretty effortless code once you have everything wired up.

Portman
A: 

I would put it into session, because in your case using the Profile means 1 additional roundtrip to DB for every DB call.

If you access the profile anyway in every actionmethod then you should use it offcourse.

Malcolm Frexner