Authorizing User with LinkedIn and Get User Profile

Before some day my sir gave me work of authorizing users with their LinkedIn profiles. So i did some RnDs and found this 2 best examples in C#.

  1. LinkedInOAuth.rar.zip
  2. LinkedInAuth.rar.zip

Note: Please Give OAuth Redirect URL while registering your domain with LinkedIn for Key and Secret.

And in this example you will get User Profile as XML so use this code to make this xml to table format.

Here Response is a XML String.

StringBuilder output = new StringBuilder();
using (XmlReader reader = XmlReader.Create(new StringReader(response)))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
DataTable dtable = new DataTable();
DataRow dRow = dtable.NewRow();
string strElement = “”;

int count = 0;
bool enter = true;

while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
if (count == 0)
{
DataColumn column = new DataColumn(reader.Name);
dtable.Columns.Add(column);
strElement = reader.Name;
count++;
}
foreach (DataColumn dc in dtable.Columns)
{
if (dc.ColumnName.Equals(reader.Name))
{
enter = false;
}
}
if (enter)
{
DataColumn column = new DataColumn(reader.Name);
dtable.Columns.Add(column);
strElement = reader.Name;
}
enter = true;
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
dRow[strElement] = reader.Value;
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
dtable.Rows.Add(dRow);
GridView1.DataSource = dtable;
GridView1.DataBind();
}
}

Advertisements
By Amar Trivedi Posted in ASP.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s