title: MVC 3 with Forms Authentication, need userid in data project, best practices?
body: <p>I just started an MVC 3 project using Forms Authentication. I have a web project (mvc project), and a data project. The data project handles all database interactions.</p>
  
  <p>I need to store the current user id in my database on every insert and update. </p>
  
  <p>Here's what I am doing right now. I feel like this has to be a common scenario. Can anyone shed some light on a better way to do this?</p>
  
  <ol>
  <li>On LogIn or Register, I grab my current user record (by username) from the database and store the user id in session.</li>
  <li>When saving a record within my data project, I refer to the httpcontext (if it exists) and use the user id that is stored in session.</li>
  </ol>
  
  <p>This works for all but one scenario. It fails when a user has a valid cookie saved, and so they are allowed to bypass the login/register action. When this happens, obviuosly, my session variable for userId is null. My next step would be to create a base controller and imlement OnAuthorizeStarting to check if my Session variable is null or not. This feels kludgy, and I'm hoping there is a better way.</p>
  
  <p>I do not want to have to pass the user Id to the data project as a parameter on every Save call.</p>
  
  <p>Anyone have some brilliance? thanks.</p>