How To Login With LinkedIn In MVC

Let’s first establish what the purpose of this code is, in the first place. For this article, the purpose of the code is to Sign in / Login with LinkedIn, in MVC.

Step 1

First, you need to create an empty MVC application.

  1. On the File menu, click New Project.

  2. In the New Project dialog box, under Project types, expand Visual C#, and then click Web. In the Name box, type "DemoLinkedINLogin", then click on OK.

  3. Now, in the dialog box, click on "MVC" under the ASP.NET 4.5.2 Templates. Then, click on "Change Authentication" on the center of the right side. Select "No Authontication".

Step 2

For signing in / logging in with LinkedIn, first of all we need to create an app in our LinkedIn & then, we need client_id and client_secret of that app. So, here we go for creating an app in Google account.

  1. Log into your LinkedIn account. Use the below link for the same.

    Developer LinkedIN Account

  2. Click on "My Apps" on top menu.


  3. Now, create an app for login API. Click on "Create Application".


  4. Fill all the fields for our new application.

    Note - Enter your localhost URL in "Website URL". After filling the form, click on "Submit" button.

    Now, we need to enter the redirect URL for OAuth 2.0 -- Authorized Redirect URLs:

  5. Finally, you got your client_id and client_secret. As per your need,   select "Default Application Permissions".

Step 3

Now, it's Code Time! Before we start the code, we need to note that LinkedIn Login API relies on OAuth 2.0 protocol for granting access.

  1. So, we need to install RestSharp Library because we use RestAPI for OAuth 2.0. For the same, go to Tools > NuGet Package Manager > Package Manager Console.

    Type "Install-Package Restsharp" and hit Enter.


  2. Then, we need to request the Authorization Code.



    1. response_type: code  
    2. client_id: Your client id  
    3. redirect_uri: Return url of your website  
    4. scope: r_basicprofile  
    Create an Action Method as Below.
    1. public ActionResult LinkedIN() {  
    2.     //Need to install below library  
    3.     //Install-Package Restsharp  
    4.     return View();  
    5. }  
    6. Add View  
    7. for that action method Paste Below code in LinkedIN.cshtml < html > < head > < title > LinkedIN Login API < /title> < /head> < body > < div > < a href = " your client id here&redirect_uri=enter your redirect url here&state=987654321&scope=r_basicprofile"  
    8. class = "btn btn-primary" > Hureee!Login with LinkedIN < /a> < /div> < /body> < /html>  

  3. So. we have already requested for an Authorization code in the above code. Now, we need to handle Response of that request. We got code and state parameter in response.

    By using the below code, we can get "access_token". Then, by using access_token, we can get the basic information about the user.

    Here we go.
    1. Create an ActionResult method to handle Authorization.  
    2. public ActionResult LinkedINAuth(string code, string state) {  
    3.     //This method path is your return URL  
    4.     return View();  
    5. }  

    First, we need to get access_token for same we use RestAPI


    1. grant_type: authorization_code  
    2. code: code that is get from Authorization response  
    3. redirect_uri: Return url of your website  
    4. client_id: your client id  
    5. client_secret: your client secret  
    6. public ActionResult LinkedINAuth(string code, string state) {  
    7.     //This method path is your return URL  
    8.     try {  
    9.         //Get Accedd Token  
    10.         var client = new RestClient("");  
    11.         var request = new RestRequest(Method.POST);  
    12.         request.AddParameter("grant_type""authorization_code");  
    13.         request.AddParameter("code", code);  
    14.         request.AddParameter("redirect_uri""http://localhost:57633/Home/LinkedINAuth");  
    15.         request.AddParameter("client_id""your client id here");  
    16.         request.AddParameter("client_secret""your client secret here");  
    17.         IRestResponse response = client.Execute(request);  
    18.         var content = response.Content;  
    19.     } catch () {  
    20.         throw;  
    21.     }  
    22.     return View();  
    23. }  

    We got the access_token. Now, we get "basic profile detail" of user based on access_token using RestAPI.


    1. access_token: access token that is get from above response  
    2. code: code that is get from Authorization response  
    3. format: json  
    4. public ActionResult LinkedINAuth(string code, string state) {  
    5.     //This method path is your return URL  
    6.     try {  
    7.         //Get Accedd Token  
    8.         var client = new RestClient("");  
    9.         var request = new RestRequest(Method.POST);  
    10.         request.AddParameter("grant_type""authorization_code");  
    11.         request.AddParameter("code", code);  
    12.         request.AddParameter("redirect_uri""http://localhost:57633/Home/LinkedINAuth");  
    13.         request.AddParameter("client_id""your client id here");  
    14.         request.AddParameter("client_secret""your client secret here");  
    15.         IRestResponse response = client.Execute(request);  
    16.         var content = response.Content;  
    17.         //Get Profile Details  
    18.         client = new RestClient("" + linkedINVM.access_token + "&format=json");  
    19.         request = new RestRequest(Method.GET);  
    20.         response = client.Execute(request);  
    21.         content = response.Content;  
    22.     } catch () {  
    23.         throw;  
    24.     }  
    25.     return View();  
    26. }  
All done....