Thursday 1 December 2016

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.

    linkedin

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

    linkedin

  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.

    linkedin

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

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

    linkedin

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.

    linkedin

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

    URL(GET)

    https://www.linkedin.com/oauth/v2/authorization

    Parameter

    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 = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=enter 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

    URL(POST)

    https://www.linkedin.com/oauth/v2/accessToken

    Parameter
    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("https://www.linkedin.com/oauth/v2/accessToken");  
    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.

    URL(POST)

    https://api.linkedin.com/v1/people/~?oauth2_

    Parameter
    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("https://www.linkedin.com/oauth/v2/accessToken");  
    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("https://api.linkedin.com/v1/people/~?oauth2_access_token=" + 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....


No comments:

Post a Comment