User Authentication with LinkedIn in ASP.Net Core 2.0 MVC

Posted by in ASP.NET Core

In this blog post I’ll show you how to authenticate with LinkedIn using ASP.Net Core 2.0 MVC. With .net core 2.0 core team deprecated  apis used in previous versions (1.0 and 1.1) and they have provided new set of apis to work with in .net core 2.0. Still you can work with old apis but old api will be removed in next versions. In this post I’ll show you how to authenticate and authorize your users with LinkedIn OAuth2 API.

First create an application in LinkedIn  developer portal.  Go to My Apps from navigation menu then click on “Create Application” button  to create new application.  Fill all required details. Then click “Submit” button to redirect to your LinkedIn application page. This page you will find “Client ID” and “Client Secret”. It is important that you keep these information safe. You need these values to connect your .net core mvc application to LinkedIn.

Next create a ASP.NET Core  MVC web application in visual studio. If you have installed multiple .net core sdks, select ASP.NET Core 2.0 framework from the drop down and make sure that you set “No Authentication” as authentication type. Then click “OK” button.

Now you need to add the our authorized redirect url in LinkedIn App. To do that Go to your ASP.NET Core MVC web project properties (Alt+Enter) then select “Debug” pane from properties panel. Then copy the “App URL” value under “Web Server settings“.  Then go to your LinkedIn application page and add it to  “

Now your can  add LinkedIn “Client ID” and “Client Secret” values to “appsettings.json” file in your project. Adding to appsettings.json file allow you to update these values without any code change.

Open “Statup.cs” file then update “ConfigureServices” method as following.

In “UserInformationEndpoint” url option you can specify what claims you need to get from LinkedIn. You can find all the available claims in LinkedIn Docs.

Update the “Configure” method  as following.

Next add an empty MVC controller name “AuthController” to your project. Then add two new action methods for login and logout. You need to update the methods as following

Add “[Authorize]” attribute to Home Controller. Only Authenticated users can access “Home/Index”  url from browser.

Next you need to add a login and logout buttons to your view. I used LinkedIn Social Icon for as login button.



Now you can run the application. Click the “Sign in with LinkedIn” button to redirect to LinkedIn. Enter your credentials and login to LinkedIn. Then LinkedIn will prompt you to “Allow” or “Cancel” your request. Click “Allow” button to complete the authentication. Ones you authenticated LinkedIn will redirect back to your application. Now you will see the “Logout” button. Now you are authenticated you can access all authorized actions and controllers.

If you try to cancel the authentication you will be get an exception saying “user_cancelled_authorize“. I’ll handle this exception using “OAuthEvent” called “OnRemoteFailure“. Even though we authenticated from LinkedIn we didn’t access any profile information from LinkedIn. To access profile information we need to add “OnCreatingTicket”  OAuth event to our application. You need to update your “ConfigureServices” method in “Startup.cs” file as following.

Update the following views files to show retrieved details from LinkedIn.



Now you run the application you should get user details of authenticated user.