Let's start with the first one:
[HttpGet("ExternalLogin/{provider}")]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
switch (provider.ToLower())
{
case "facebook":
// case "google":
// case "twitter":
// todo: add all supported providers here
// Redirect the request to the external provider.
var redirectUrl = Url.Action(
nameof(ExternalLoginCallback),
"Token",
new { returnUrl });
var properties =
SignInManager.ConfigureExternalAuthenticationProperties(
provider,
redirectUrl);
return Challenge(properties, provider);
default:
// provider not supported
return BadRequest(new {
Error = String.Format("Provider '{0}' is not
supported.", provider)
});
}
}
The code is quite straightforward. The first thing we're doing here is checking whether the given provider is among those supported by our application; if that's the case, we redirect the request to the external provider using a dedicated interface provided by the ASP.NET Core Identity authentication service; otherwise, we return a Bad Request.
It's worth noting that this controller won't be limited to Facebook; as a matter of fact, it will act as a common interface to deal with (almost) any external provider we might like to add in the future, as long as it's supported by ASP.NET Core.