For more about OAuth in the real world, refer to my article in the Free DNC Magazine Issue 4.
Registering with LinkedInBefore we can start using LinkedIn as an OAuth for us, we have to register an ‘Application’ with Linked in.
- Log in to http://developer.linkedin.com/. It requires you have a LinkedIn account and will link the account with your developer account.
- Once you Log in, select API Keys to create an application and get a set of API Keys
- The Application creation page takes a host of information of which phone number is mandatory
- Note we’ve provided Website URL as Localhost loop back address.
- Click on Add Application to create the Application and retrieve the ApiKey and Secret Key.
- You are now all set to build your ASP.NET Web App with LinkedIn Authentication
Configuring you ASP.NET Project1. Start a new ASP.NET project and Select ASP.NET MVC4 Template Project Type. Use the Internet Template.
2. In the web.config add the ApiKey and the Secret Key as two Key/Value pairs in the <appSettings> sections. E.g.
… (removed for brevity)
<add key="apiKey" value="APIKey FromLinkedInAppRegistration"/>
<add key="apiSecret" value="SecretKey FromLinkedInAppRegistration"/>
Go to App_Start\AuthConfig.cs and add the following line to setup the LinkedInClient provided by ASP.NET
4. That’s all the configuration that’s required
Logging in Using LinkedIn1. Run the Application and click on Login. The following screen will come up
2. Click on LinkedIn to be redirected to the LinkedIn Login Page
Notice the name you provided for your application has come up - “ASP.NET OAuth Demo”. Login using a valid LinkedIn credential and you will be redirected back to the App
3. SPLAT!!! Application refuses to login!
4. Well fear not, you didn’t do anything wrong. Just that the default LinkedInClient (version before 4.1.4.x) shipped with a bug, which has since been fixed. So go to Nuget Package Management Console and update your DotNetOpenAuth.Core package. As we can see below it’s a big update.
5. After update Repeat from Step 1 and instead of failure on Step 4. You should see the following:
6. Provide a User name and select Register to get Authenticated into the Application.
7. Well, there are no more steps. Here on, if you log off of close browser you can log in LinkedIn Id. You will not be asked to Register if the Linked Id you used already exists in the Database.
ConclusionBarring a small bug in the default client, it is pretty easy to use LinkedIn as an OAuth provider in your ASP.NET MVC Application.