title: Register External Login Web API
body: <p>I don't understand why their isn't a clear tutorial or guideline on this, so I hope my question can be answered here.</p>
  
  <p>So, trying to register users from facebook or google, via the Web Api.</p>
  
  <p>The problem is, at the <code>RegisterExternal</code> method, on this line: </p>
  
  <pre><code>var info = await Authentication.GetExternalLoginInfoAsync();
  </code></pre>
  
  <p>It returns null, and thus returning a <code>BadRequest()</code></p>
  
  <p><strong>What I got so far:</strong></p>
  
  <p>In <code>Startup.Auth.cs</code> I've hadded the id's and the secrets, note that I have also tried using <code>Microsoft.Owin.Security.Facebook</code></p>
  
  <pre><code>var facebookOptions = new Microsoft.Owin.Security.Facebook.FacebookAuthenticationOptions
              {
                  AppId = "103596246642104",
                  AppSecret = "1c9c8f696e47bbc661702821c5a8ae75",
                  Provider = new FacebookAuthenticationProvider()
                  {
                      OnAuthenticated = (context) =&gt;
                      {
                          context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, ClaimValueTypes.String, "Facebook"));
  
                          return Task.FromResult(0);
                      }
                  },
              };
              facebookOptions.Scope.Add("email");
              app.UseFacebookAuthentication(facebookOptions);
  
  
  
              app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
              {
              ClientId = "328779658984-t9d67rh2nr681bahfusan0m5vuqeck13.apps.googleusercontent.com",
              ClientSecret = "ZYcNHxBqH56Y0J2-tYowp9q0",
              CallbackPath = new PathString("/api/Account/ManageInfo")
          });
  </code></pre>
  
  <p>facebookOptions source: <a href="https://stackoverflow.com/a/26891357/2901207">this post</a></p>
  
  <p>That extra facebookOptions did not solve the problem.</p>
  
  <p>I am able to retrieve an access_token from both Google and Facebook. I'm also able to Authenticate with this access_token to <code>api/Account/UserInfo</code></p>
  
  <pre><code>GET http://localhost:4856/api/Account/UserInfo
  in the header:
  Authorization: Bearer R9BTVhI0...
  </code></pre>
  
  <p>Which returns: 
  <code>{"Email":"firstname lastname","HasRegistered":false,"LoginProvider":"Facebook"}</code></p>
  
  <p>One issue I notice their, is that it returns my name as Email, not the actual Email adress.</p>
  
  <p>Now I want to register the external login with a new user for my database, which I make a POST call like this:</p>
  
  <pre><code>POST http://localhost:4856/api/Account/RegisterExternal
  [header]
  authorization: bearer 6xcJoutY...
  Content-Type: application/json
  [body]
  {"Email":"...@hotmail.com"}
  </code></pre>
  
  <p>source: <a href="https://stackoverflow.com/a/21358918/2901207">this post</a></p>
  
  <p>Now this returns a BadRequest on this code snippit, inside RegisterExternal():</p>
  
  <pre><code>    public async Task&lt;ActionResult&gt; ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
      {
  if (!ModelState.IsValid)
              {
                  return BadRequest(ModelState);
              }
              //AuthenticationManger?
              var info = await Authentication.GetExternalLoginInfoAsync();
              if (info == null)
              {
                  return InternalServerError();
              }
  </code></pre>
  
  <p>In debugging, the <code>ExternalLoginConfirmationViewModel</code> does contain my email adress.</p>
  
  <p>What am I doing wrong? Do I have to add something to the <code>Startup.cs</code>? Is there something more I have to do in the <code>Startup.Auth.cs</code>? Am I incorrectly calling <code>RegisterExternal</code>? In MVC it goes so smooth, why not in the Web API?</p>
  
  <p>Aso looked at <a href="https://stackoverflow.com/a/21292772/2901207">this answer</a> from <a href="https://stackoverflow.com/questions/21167648/registering-web-api-2-external-logins-from-multiple-api-clients-with-owin-identi">this question</a>, But I didn't understand how to implement this.</p>