Using External Login in ASP.NET Identity
Last week, I wrote this blog Using ASP.NET Identity: How I added it into my MiniBlog project. In this blog, I will explain how I added the external logins for Google and GitHub.
For my blog I chose Google and GitHub because they let me create a polished, branded login experience, even as a private individual. Unlike some other providers (like Microsoft) that require you to be a registered company for verification.
Create an OAuth app/client at the external login provider
As mentioned above, I used Google and GitHub. You created an OAuth app/client at the following URLs:
Both pages will guide you through the necessary steps.
For Google and also some other providers that I didn't implement, you need a privacy and terms of use page.
Adding the NuGet packages
For Google and GitHub you need to install these packages below.
If you're using a different provider, there's probably an official Microsoft package for it. Search for Microsoft.AspNetCore.Authentication.*. If not, the AspNet.Security.OAuth.Providers project on GitHub will most likely have one.
Configure the external providers
The only thing left to do is to add your provider to your Program.cs or Startup.cs and implement it with the ClientId and ClientSecrets of your created OAuth app/client.
if (configuration["Authentication:GitHub:ClientId"] != null && configuration["Authentication:GitHub:ClientSecret"] != null)
{
builder.Services.AddAuthentication().AddGitHub(githubOptions =>
{
githubOptions.ClientId = configuration["Authentication:GitHub:ClientId"]!;
githubOptions.ClientSecret = configuration["Authentication:GitHub:ClientSecret"]!;
githubOptions.Scope.Add("user:email");
});
}
if (configuration["Authentication:Google:ClientId"] != null && configuration["Authentication:Google:ClientSecret"] != null)
{
builder.Services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = configuration["Authentication:Google:ClientId"]!;
googleOptions.ClientSecret = configuration["Authentication:Google:ClientSecret"]!;
});
}
If you notice that not everything is working you might want to check out the first part: Using ASP.NET Identity
What’s Next
This was the second part of adding ASP.NET Identity to support external logins to my MiniBlog project. In the final part of the series, I’ll show you how to extend the Identity user with custom fields, like a display name, so you can store and use additional profile information.
Comments
Register or login to leave a comment