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.
- On the File menu, click New Project.
- 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.
- 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.
- Log into your LinkedIn account. Use the below link for the same.
Developer LinkedIN Account
- Click on "My Apps" on top menu.

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

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

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

- Then, we need to request the Authorization Code.
URL(GET)
https://www.linkedin.com/oauth/v2/authorization
Parameter
Create an Action Method as Below.- response_type: code
- client_id: Your client id
- redirect_uri: Return url of your website
- scope: r_basicprofile
- public ActionResult LinkedIN() {
- //Need to install below library
- //Install-Package Restsharp
- return View();
- }
- Add View
- 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"
- class = "btn btn-primary" > Hureee!Login with LinkedIN < /a> < /div> < /body> < /html>
- 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.
First, we need to get access_token for same we use RestAPI- Create an ActionResult method to handle Authorization.
- public ActionResult LinkedINAuth(string code, string state) {
- //This method path is your return URL
- return View();
- }
URL(POST)
https://www.linkedin.com/oauth/v2/accessToken
Parameter
We got the access_token. Now, we get "basic profile detail" of user based on access_token using RestAPI.- grant_type: authorization_code
- code: code that is get from Authorization response
- redirect_uri: Return url of your website
- client_id: your client id
- client_secret: your client secret
- public ActionResult LinkedINAuth(string code, string state) {
- //This method path is your return URL
- try {
- //Get Accedd Token
- var client = new RestClient("https://www.linkedin.com/oauth/v2/accessToken");
- var request = new RestRequest(Method.POST);
- request.AddParameter("grant_type", "authorization_code");
- request.AddParameter("code", code);
- request.AddParameter("redirect_uri", "http://localhost:57633/Home/LinkedINAuth");
- request.AddParameter("client_id", "your client id here");
- request.AddParameter("client_secret", "your client secret here");
- IRestResponse response = client.Execute(request);
- var content = response.Content;
- } catch () {
- throw;
- }
- return View();
- }
URL(POST)
https://api.linkedin.com/v1/people/~?oauth2_
Parameter
- access_token: access token that is get from above response
- code: code that is get from Authorization response
- format: json
- public ActionResult LinkedINAuth(string code, string state) {
- //This method path is your return URL
- try {
- //Get Accedd Token
- var client = new RestClient("https://www.linkedin.com/oauth/v2/accessToken");
- var request = new RestRequest(Method.POST);
- request.AddParameter("grant_type", "authorization_code");
- request.AddParameter("code", code);
- request.AddParameter("redirect_uri", "http://localhost:57633/Home/LinkedINAuth");
- request.AddParameter("client_id", "your client id here");
- request.AddParameter("client_secret", "your client secret here");
- IRestResponse response = client.Execute(request);
- var content = response.Content;
- //Get Profile Details
- client = new RestClient("https://api.linkedin.com/v1/people/~?oauth2_access_token=" + linkedINVM.access_token + "&format=json");
- request = new RestRequest(Method.GET);
- response = client.Execute(request);
- content = response.Content;
- } catch () {
- throw;
- }
- return View();
- }

atulPosted Mar 14, 2020, 6:56 AM
Hi Ankit,My response is coming blank, I dont know where I am doing wrong IRestResponse response = client.Execute(request); var content = response.Content;
Ankit KanojiaPosted Mar 12, 2020, 2:20 AM
Thank you so much Raja sekhar
Ankit KanojiaPosted Mar 12, 2020, 2:20 AM
Thank you so much Raja sekhar
Ankit KanojiaPosted Mar 12, 2020, 2:20 AM
Thank you so much Ashish Upalekar
Ankit KanojiaPosted Mar 12, 2020, 2:20 AM
Thank you so much vishal joshi for being interactive
Ankit KanojiaPosted Mar 12, 2020, 2:19 AM
Have you tried with your tokens and credentials Vikas Singh? can you elloborate the actual scenerio of issue so that it helps me to resolve issue on same direction.
Ankit KanojiaPosted Mar 12, 2020, 2:18 AM
Thank you so much Vikas Singh, will revert back once i fot the resolution for the issue posted by you. By the way again thanks for being interactive.
Ankit KanojiaPosted Mar 12, 2020, 2:17 AM
Thank you so much Raja sekhar
Ankit KanojiaPosted Mar 12, 2020, 2:17 AM
Thank you so much Dheeraj Pandey
Ankit KanojiaPosted Mar 12, 2020, 2:16 AM
Rajeev Ranjan : There are many choices.. but i choose restsharp.. its light weight and simple that is the reason..
Vikas SinghPosted Mar 12, 2020, 1:12 AM
Here access_token is invalid because api responce is invalid -----------------------------------------------------------------------Var client = new RestClient("https://www.linkedin.com/oauth/v2/accessToken"); var request = new RestRequest(Method.POST); request.AddParameter("grant_type", "authorization_code"); request.AddParameter("code", code); request.AddParameter("redirect_uri", "http://localhost:57633/Home/LinkedINAuth"); request.AddParameter("client_id", "81qwzypoua0y97"); request.AddParameter("client_secret", "gH2exhTg1r6jqK1K"); IRestResponse response = client.Execute(request); var content = response.Content; //Fetch AccessToken JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); LinkedINVM linkedINVM = jsonSerializer.Deserialize<LinkedINVM>(content); //Get Profile Details client = new RestClient("https://api.linkedin.com/v1/people/~?oauth2_access_token=" + linkedINVM.access_token + "&format=json"); request = new RestRequest(Method.GET); response = client.Execute(request); content = response.Content; jsonSerializer = new JavaScriptSerializer(); LinkedINResVM linkedINResVM = jsonSerializer.Deserialize<LinkedINResVM>(content); return View(linkedINResVM);
Raja sekharPosted May 24, 2018, 6:17 AM
Hi Suchit How can i retrieve Email Address??
siva cPosted Mar 15, 2018, 2:22 AM
Hi Suchit, I tried to get the access token by this Local IIS url like request.AddParameter("redirect_uri", "http://our system ipaddress/virdirname/Home/LinkedINAuth"); but, after login i am getting the object null reference error. Could you please help on this?
Ashish UpalekarPosted Jul 21, 2017, 9:56 AM
Hi Suchit, Code is working fine for r_basic profile but when I replace it with r_fullprofile its not working. Getting null response. I have one app where I configured r_fullprofile permission. I understand that I need to expand LinkedInVM class to hold additional info. But atleast I should have got response. Do you have any code for r_fullprofile ?
Dheeraj PandeyPosted Mar 24, 2017, 8:12 AM
In above example "linkedINVM.access_token " is not define any where and thats why i am not able to gate profile detail. so please help me to get user profile detail.
Rajeev RanjanPosted Nov 30, 2016, 11:23 PM
Super article sir, but why Restsharp?there is already linkedin dll for same task