How to Integrate External Login in your Application
External provider Authentication is a must-have feature in the new modern days Applications, as users can easily register new accounts and Login using their very handy social account Credentials
What are External Providers?
External providers are the third-party authenticators which are used in your application to achieve Authentication.
What happens behind the curtains?
Web applications redirect users to sign in to the selected external provider such as Facebook, Google, LinkedIn, Microsoft, and others which in turn redirects back to a call-back URL providing basic profile information such as email address, full name, and username.
This information is used to register a new account or sign in to the user who is already registered.
How do we perform this magic?
We will break this process into 3 easy steps
- Creating an application on an external developer’s portal
- Configure the call back URL properly
- Use it in our very own ASP.NET project
Creating an application on an external developer’s portal
These External providers such as Google, Microsoft, Facebook, LinkedIn, GitHub, etc have their developer’s portal
Microsoft: Azure portal – App registrations
GitHub: GitHub OAuth Apps
You just navigate to the developer app URL and create an application for the website you want to add external authentication. The required details are nothing but the URL of your website.
Copy App Secret and App ID or Client Secret or Client ID from the Developers portal (We will use them in our Startup.cs file)
Configure the call back URL properly
This is an important step in the process, this URL will be hit when you log in from an external login provider.
You must set the Authorized redirect URL in the app which points to the route of your website which will look like
https://localhost:XXXX/signin-google if you are using google authentication
https://localhost:XXXX/signin-facebook if you are using Facebook authentication
similarly for other external providers you will have your redirect URL.
Use it in our very own ASP.NET project
If you have not created the project, Create one.
Add a NuGet package for Authentication, for example, here we want to add Google Authentication, we will install
Microsoft.AspNetCore.Authentication.Google. Similarly, you will find packages according to your external providers.
Add this code to your start-up File
Now here options.LoginPath redirects to a method in the Account controller. I have made two methods in the Account Controller to retrieve the Login Information.
And it’s done! Your App now has an External Google Login Functionality.