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
- response_type: code
- client_id: Your client id
- redirect_uri: Return url of your website
- scope: r_basicprofile
Create an Action Method as Below.
- public ActionResult LinkedIN() {
-
-
- 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.
- Create an ActionResult method to handle Authorization.
- public ActionResult LinkedINAuth(string code, string state) {
-
- return View();
- }
First, we need to get access_token for same we use RestAPI
URL(POST)
https://www.linkedin.com/oauth/v2/accessToken
Parameter
- 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) {
-
- try {
-
- 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();
- }
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
- 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) {
-
- try {
-
- 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;
-
- 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();
- }
All done....