title: Check Login in MasterPage before ANY other code is processed VB.NET
body: <p>I'm developing an application which requires log in.</p>
  
  <p>I'm using Master Pages and a customer Web Control which is my main menu system.</p>
  
  <p>The main menu accesses database information to provide notifications and various things, none of which will work unless the user is logged in.</p>
  
  <p>I am trying to place the following code:</p>
  
  <pre><code>If Session("hgAdminUser") = "" And InStr(Request.ServerVariables("url"), "/Login.aspx") &lt;= 0 Then _
              Response.Redirect("Login.aspx") ' if userSession is empty, redirect to login page.
  </code></pre>
  
  <p>Into the top of my MasterPage code behind, but it's still trying to process scripts, the main problem being that it is running the .ascs file which requires Database access, which it can't do without the required parameters from Login.</p>
  
  <p>I've tried running it within a Page_PreInit sub, also Page_Load and Page_Init, but in all cases it ignores this (or probably doesn't ignore it, but errors come in before it deals with this) and returns a big fat ERROR due to functions that I don't even want it to be running!!</p>
  
  <p>So basically, my question is, how can I check for a session variable, which if not present cause all other scripts on the page to be ignored and redirect?</p>
  
  <p>This is all using VB.NET</p>