In this article I will explain how to login with LinkedIn Account in ASP.Net Website and get LinkedIn User Profile details like ID, Name, Profile Picture, Email Address, etc. using the free ASPSnippets.LinkedInAPI.
This article also explains how create a LinkedIn Application and get the LinkedIn API Key and Secret Key on LinkedIn Developers Site.
Get LinkedIn API Key and Secret Key on LinkedIn Developers Site
In order to create an application you need to visit the Linked Developer site using the following URL.
There you need to click Add New Application button.
After that you need to fill the following form, inside which the most important section is the Default Scope where you need to choose what User details you want to fetch through your application.
Once the form is submitted, you will get the LinkedIn API Key and Secret Key as shown below.
Downloading and referencing Free ASPSnippets LinkedIn API
You can download the free ASPSnippets LinkedIn API from following URL
And either add it using Add Reference or copy it inside the BIN folder of your website.
HTML Markup
In the below HTML Markup, I have an ASP.Net Button which triggers the LinkedIn Authorization process. Once the user profile details are fetched, it will be displayed using the Label and Image controls.
<asp:Button Text="Login with LinkedIn" runat="server" OnClick="Authorize" />
<asp:Panel ID="pnlDetails" runat="server" Visible="false">
<hr />
<asp:Image ID="imgPicture" runat="server" /><br />
Name:
<asp:Label ID="lblName" runat="server" /><br />
LinkedInId:
<asp:Label ID="lblLinkedInId" runat="server" /><br />
Location:
<asp:Label ID="lblLocation" runat="server" /><br />
EmailAddress:
<asp:Label ID="lblEmailAddress" runat="server" /><br />
Industry:
<asp:Label ID="lblIndustry" runat="server" /><br />
Headline:
<asp:Label ID="lblHeadline" runat="server" />
</asp:Panel>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using ASPSnippets.LinkedInAPI;
VB.Net
Imports System.Data
Imports ASPSnippets.LinkedInAPI
LinkedIn Authorization and fetching the LinkedIn User Profile details
The very first thing you need to do is set the LinkedIn API Key and Secret Key to its respective properties.
Inside the Button click event handler, the Authorize method is called which will redirect user to the LinkedIn Website where he will login and also grant permissions to the application to fetch his profile details.
Once the user has authorized the LinkedIn Profile details are fetched using the Fetch method as DataSet object and are displayed on page using Image and Label controls.
C#
protected void Page_Load(object sender, EventArgs e)
{
LinkedInConnect.APIKey = "<Your LinkedIn API Key>";
LinkedInConnect.APISecret = "<Your LinkedIn API Secret>";
LinkedInConnect.RedirectUrl = Request.Url.AbsoluteUri.Split('?')[0];
if (LinkedInConnect.IsAuthorized)
{
pnlDetails.Visible = true;
DataSet ds = LinkedInConnect.Fetch();
imgPicture.ImageUrl = ds.Tables["person"].Rows[0]["picture-url"].ToString();
lblName.Text = ds.Tables["person"].Rows[0]["first-name"].ToString();
lblName.Text += " " + ds.Tables["person"].Rows[0]["last-name"].ToString();
lblEmailAddress.Text = ds.Tables["person"].Rows[0]["email-address"].ToString();
lblHeadline.Text = ds.Tables["person"].Rows[0]["headline"].ToString();
lblIndustry.Text = ds.Tables["person"].Rows[0]["industry"].ToString();
lblLinkedInId.Text = ds.Tables["person"].Rows[0]["id"].ToString();
lblLocation.Text = ds.Tables["location"].Rows[0]["name"].ToString();
imgPicture.ImageUrl = ds.Tables["person"].Rows[0]["picture-url"].ToString();
}
}
protected void Authorize(object sender, EventArgs e)
{
LinkedInConnect.Authorize();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
LinkedInConnect.APIKey = "<Your LinkedIn API Key>"
LinkedInConnect.APISecret = "<Your LinkedIn API Secret>"
LinkedInConnect.RedirectUrl = Request.Url.AbsoluteUri.Split("?")(0)
If LinkedInConnect.IsAuthorized Then
pnlDetails.Visible = True
Dim ds As DataSet = LinkedInConnect.Fetch()
imgPicture.ImageUrl = ds.Tables("person").Rows(0)("picture-url").ToString()
lblName.Text = ds.Tables("person").Rows(0)("first-name").ToString()
lblName.Text += " " + ds.Tables("person").Rows(0)("last-name").ToString()
lblEmailAddress.Text = ds.Tables("person").Rows(0)("email-address").ToString()
lblHeadline.Text = ds.Tables("person").Rows(0)("headline").ToString()
lblIndustry.Text = ds.Tables("person").Rows(0)("industry").ToString()
lblLinkedInId.Text = ds.Tables("person").Rows(0)("id").ToString()
lblLocation.Text = ds.Tables("location").Rows(0)("name").ToString()
imgPicture.ImageUrl = ds.Tables("person").Rows(0)("picture-url").ToString()
End If
End Sub
Protected Sub Authorize(sender As Object, e As EventArgs)
LinkedInConnect.Authorize()
End Sub
Demo
Downloads