title: Problems with MVC4 & External Login with Google
body: <p>I've an ASP .NET MVC4 application that uses SimpleMembership and allows users to sign in with their google accounts.</p>
  
  <p>In order to do that I configured the application's AuthConfig class by uncommenting the <code>OAuthWebSecurity.RegisterGoogleClient();</code> line to add google as an external login method. I didn't really change much inside the external login methods that already come with the <em>ASP .NET MVC4 Internet Application</em> template.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Everything works fine when I run it locally (IIS express) or when I deploy it to a 'test' server (IIS 6.1, accessible on the intranet only). When I click my "sign in with google" button, I'm sent to google's authentication page, whose url looks something like this:</p>
  
  <pre><code>https://accounts.google.com/ServiceLogin?service=lso&amp;passive=12096...(long url)
  </code></pre>
  
  <p>Alas, when I deploy it to the 'production' server (internet facing) and try to access it via internet, Google authentication stops working: when I click my "sign in with google" I'm being sent to an url that looks like this:</p>
  
  <pre><code>http://MY_APPLICATION_NAME.MY_DOMAIN.com/accounts/o8/ud?openid.claimed_id=http%3A%2F%2Fspecs.openid...(long url)
  </code></pre>
  
  <p>This url is wrong not only because it has my domain in it (I believe it should be accounts.google.com?), but also because the rest of it is completely different from the url I get when the authentication works.</p>
  
  <p><strong>What I know and have tried:</strong></p>
  
  <p>If I use fiddler, I can see that the application is behaving differently when I'm accessing the application via internet or intranet.</p>
  
  <ul>
  <li>If I access it from within the network, I see I'm making a request to <code>http://MY_APPLICATION_NAME.MY_DOMAIN.com/account/ExternalLogin?returnUrl=</code> and then <code>"Tunnel to www.google.com:443, Tunnel to accounts.google.com:443"</code>. This works ok.</li>
  <li>If I access it from the internet I see I'm making a request to <code>http://MY_APPLICATION_NAME.MY_DOMAIN.com/account/ExternalLogin?returnUrl=</code> and then another one to <code>http://MY_APPLICATION_NAME.MY_DOMAIN.com/accounts/o8/ud?openid.claimed_id=http%3A%2F%2Fspecs.openid...(long url)</code>. This obviously gives a 404.</li>
  </ul>
  
  <p>I might be wrong but I assume that this url is generated by the <code>OAuthWebSecurity.RequestAuthentication(provider, returnUrl)</code>. What puzzles me is that it looks like it is requesting the authenticaton to my server instead of accounts.google.com.</p>
  
  <p>My research so far has only brought to me posts/questions/etc. showing how easy it is to use SimpleMembership for external authentication and I can see everyone does it pretty much the same way. </p>
  
  <p><strong>Conclusion:</strong></p>
  
  <p>I believe this probably has something to do with something I must configure on web.config or IIS. I do know that the requests to my application arrive at my DNS server which does a url rewrite and forwards the request to my web server, although I don't know if this is related to the problem.</p>
  
  <p>Does anyone have any idea of what might be wrong or ever had a similar issue? Any help would be appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've looked into <a href="https://github.com/matejskubic/dotnetopenid/blob/master/src/DotNetOpenAuth.AspNet/OpenAuthSecurityManager.cs" rel="nofollow">OpenAuthSecurityManager.cs</a>'s source code and although I can't debug it I see the problem might be related to an incorrect HttpContext inside the RequestAuthentication method, due to my reverse proxy settings on IIS?</p>