Connect to LinkedIn using OAuth in an ASP.NET MVC Application




ASP.NET 4.0 introduced OAuth integration using the DotNetOpenAuth library. We can use it to authenticate our ASP.NET application using various OAuth providers. Today we will see how we can Authenticate using the built in LinkedIn client.

For more about OAuth in the real world, refer to my article in the Free DNC Magazine Issue 4.

Registering with LinkedIn

Before 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

01-ApiKeys

- The Application creation page takes a host of information of which phone number is mandatory

01-RegistrationPage1

- 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 Project

1. 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.

<appSettings>
… (removed for brevity)
<add key="apiKey" value="APIKey FromLinkedInAppRegistration"/>
<add key="apiSecret" value="SecretKey FromLinkedInAppRegistration"/>

</appSettings>

Go to App_Start\AuthConfig.cs and add the following line to setup the LinkedInClient provided by ASP.NET

03-AuthConfigChanges

4. That’s all the configuration that’s required

Logging in Using LinkedIn

1. Run the Application and click on Login. The following screen will come up

04-Login-Page

2. Click on LinkedIn to be redirected to the LinkedIn Login Page

05-OAuth-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
06-LinkedIn-Login-Failuer

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.

07-DotNetOpenAuth-Updates

5. After update Repeat from Step 1 and instead of failure on Step 4. You should see the following:

08-Register-Linked-In

6. Provide a User name and select Register to get Authenticated into the Application.

09-Logged-In

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.

Conclusion

Barring a small bug in the default client, it is pretty easy to use LinkedIn as an OAuth provider in your ASP.NET MVC Application.

Will you give this article a +1 ? Thanks in advance




comments

1 Response to "Connect to LinkedIn using OAuth in an ASP.NET MVC Application"
  1. TomTom said...
    November 22, 2013 at 6:18 AM

    This is a very useful tutorial. I have just one Problem:

    When I Start the Project and click on "Register with LinkedIn" I get the following Error:

    DotNetOpenAuth.Messaging.ProtocolException is an Exception of type "DotNetOpenAuth.Core.dll"

    But I have not seen any information to catch the exception. Maybe you can help me?

    Thanks in advance!!!

 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions