title: asp.net c# redirecting from http to https
body: <p>So in my code I want to detect if my login page is being called http, and redirect it to https.</p>
  
  <p>I know there are non code ways to skin this cat, but for frustrating technical reasosn I'm backed into doing it in code.</p>
  
  <pre><code>            if (!Request.IsSecureConnection)
              {
                  string redirectUrl = Request.Url.ToString().Replace("http:", "https:");
                  Response.Redirect(redirectUrl);
              }
  </code></pre>
  
  <p>So I drop this in my <code>Page_Load(...)</code>, make sure my debugger uses real IIS, not VS2008s IIS, and hit debug.</p>
  
  <p>Inthe debugger, waltz along, hit 
  Response.Redirect("<a href="https://localhost/StudentPortal3G/AccessControl/AdLogin.aspx" rel="noreferrer">https://localhost/StudentPortal3G/AccessControl/AdLogin.aspx</a>"), 
  hit f5.</p>
  
  <p>Get "Internet Explorere Cannot Display the webpage, url is HTTP, not HTTPS.
  Not getting an informative error... same thing happens not running in the debugger.</p>
  
  <p>So what am I missing? it does not appear to be rocket science, I've seen similar code on lots of blogs...</p>
  
  <p>What am I doing wrong? I figure it has to be a totally obvious Rookie mistake, but I'm not seeing it.</p>