Add twitter login authentication to website in

Posted by imomins on October 8, 2012 at 1:40 AM

In this article I will explain how to create app in twitter and implement twitter login authentication for website in
In previous post I explained article how to integrate facebook login authentication for website in Now I will explain how to allow users to login with twitter accounts in website using

Before implement twitter login authentication we need to get consumerKey and consumerSecret key from twitter for that we need to create application in twitter by using this link  once open that will display window like this

Once app page opened enter Application Name, Description, website (Ex: and callback url details and click create new application button and here one more thing we need to remember is twitter won’t support for localhost sites (ex: http://localhost/Default.aspx) because of that we need to give hosted domain site url. 

If you want to test this with your local application no worries check this post how host website in IIS with custom URL .Once our app created in twitter that would be like as shown below image here we can change logo of our application

Now create new application using visual studio and write following code aspx page

<html xmlns="" >
<head id="Head1">
<title>Twitter Login Authentication for Website in</title>
<form id="form1" runat="server">
<asp:ImageButton ID="imgTwitter" runat="server" ImageUrl="~/TwitterSigning.png"
onclick="imgTwitter_Click" />
<table id="tbleTwitInfo" runat="server" border="1" cellpadding="4" cellspacing="0" visible="false">
<td colspan="2"><b>Twitter User Profile</b></td>
<td><b>Full Name:</b></td>
<td><b>Profile Image:</b></td>
<td><img src="<%=profileImage%>" /></td>
<td><b>Twitter Followers:</b></td>
<td><b>Number Of Tweets:</b></td>
<td><b>Recent Tweet:</b></td>
Now in code behind add following namespaces

C# Code

using System;
using System.Web.UI;
using System.Xml;
using oAuthExample;
After completion of adding namespaces write following code in code behind

string url = "";
string xml = "";
public string name = "";
public string username = "";
public string profileImage = "";
public string followersCount = "";
public string noOfTweets = "";
public string recentTweet = "";

protected void Page_Load(object sender, EventArgs e)
private void GetUserDetailsFromTwitter()
if(Request["oauth_token"]!=null & Request["oauth_verifier"]!=null)
imgTwitter.Visible = false;
tbleTwitInfo.Visible = true;
var oAuth = new oAuthTwitter();
//Get the access token and secret.
oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
if (oAuth.TokenSecret.Length > 0)
//We now have the credentials, so make a call to the Twitter API.
url = "";
xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty);
XmlDocument xmldoc=new XmlDocument();
XmlNodeList xmlList = xmldoc.SelectNodes("/user");
foreach (XmlNode node in xmlList)
name = node["name"].InnerText;
username = node["screen_name"].InnerText;
profileImage = node["profile_image_url"].InnerText;
followersCount = node["followers_count"].InnerText;
noOfTweets = node["statuses_count"].InnerText;
recentTweet = node["status"]["text"].InnerText;
protected void imgTwitter_Click(object sender, ImageClickEventArgs e)
var oAuth = new oAuthTwitter();
if (Request["oauth_token"] == null)
//Redirect the user to Twitter for authorization.
//Using oauth_callback for local testing.
oAuth.CallBackUrl = "";
If you observe above code I used oAuthTwitter class file you can get this class file from downloadable code. Now get consumerKey and consumerSecret key from twitter and add it in web.config file like this

<add key="consumerKey" value="Gyew474of7tpEBqnpDw" />
<add key="consumerSecret" value="ytgklq3b8lkxgPShCWeawqzrYpUa1bgsaeGRwW" />

 Download sample code attached

Categories: ASP.NET, C#, Social Networking

Post a Comment


Oops, you forgot something.


The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register