This post is going to cover adding authentication using Twitter to the same project that has been used in all of my IdentityServer examples. The same basic idea would apply to almost any third party authentication setup so this should give you a good starting point for any integration. The starting point of the code can be found here.
Create Twitter App
Before any code changes create a new application on Twitter via this page. Click Create New App to begin the process.
On the Create an application page enter all the requested information. Note that the website won’t allow a localhost address. If you don’t have a real address for your application just enter a random URL as I did here. When finished click Create your Twitter application.
Now that we have an application click on the Keys and Access Tokens tab. We will need both the Consumer Key and Consumer Secret when we get to the Identity Application.
Identity Application Changes
Now that we have a Twitter application ready to go let us dive into the changes needed to the Identity Application. The first step is to add a reference to Microsoft.AspNetCore.Authentication.Twitter via NuGet.
Next in the ConfigureServices function of the Startup class after app.UseIdentityServer() add the following.
AuthenticationScheme = "Twitter",
DisplayName = "Twitter",
SignInScheme = "Identity.External",
ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"],
ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"]
The first three options should a straight forward enough. The next two are the values from the Twitter application I mentioned above. In this example, I am storing the values using User Secrets which get pulled out of configuration. For more details on how to set up secrets, you can see this post.
The above are all the changes required. The Identity Application will now allow users to auth using Twitter.
Logging in using Twitter
As you can see below the login page now has a button for Twitter.
When the user chooses to log in using Twitter they are shown the following page where they must approve access to their Twitter account from your application.
If this is the first time a user has logged in with Twitter they will be prompted to enter an email address to finish registration.
As you can see adding external authentication is super simple. Check out the Microsoft Docs on Twitter Auth (ASP.NET Core 2.0 so look out for differences if you are not on the preview bits) and IdentityServer Docs on External Auth for more information.
The finished code can be found here.