In this article I will explain with an example, how to do integration of Twitter API in ASP.Net website using C# and VB.Net.
The Twitter API will be integrated in ASP.Net website with the help of ASPSnippets Twitter API in C# and VB.Net.
 
Create Twitter Application and get API Key and API Secret
You will need to create an application on Twitter Apps and get an API Key and API Secret.
 
HTML Markup
The HTML Markup consist of a Login Button and an HTML Table with the fields to populate the Twitter User Account and Profile details of the Logged in User as well as some other User whose Twitter Id or Handle is being passed.
<asp:Button ID="btnLogin" runat="server" Text="Login with Twitter" OnClick="btnLogin_Click" />
<hr />
<table runat="server" id="tblTwitter" visible="false">
    <tr>
        <td colspan="2">
            <u>Logged in Twitter User's Profile</u>
        </td>
    </tr>
    <tr>
        <td style="width: 100px">
            Profile Image
        </td>
        <td>
            <asp:Image ID="imgProfile" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Name
        </td>
        <td>
            <asp:Label ID="lblName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Twitter Id
        </td>
        <td>
            <asp:Label ID="lblTwitterId" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Screen Name
        </td>
        <td>
            <asp:Label ID="lblScreenName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Description
        </td>
        <td>
            <asp:Label ID="lblDescription" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Tweets
        </td>
        <td>
            <asp:Label ID="lblTweets" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Followers
        </td>
        <td>
            <asp:Label ID="lblFollowers" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Friends
        </td>
        <td>
            <asp:Label ID="lblFriends" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Favorites
        </td>
        <td>
            <asp:Label ID="lblFavorites" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Location
        </td>
        <td>
            <asp:Label ID="lblLocation" runat="server" />
        </td>
    </tr>
</table>
<br />
<table runat="server" id="tblOtherTwitter" visible="false">
    <tr>
        <td colspan="2">
            <u>Other Twitter User's Profile</u>
        </td>
    </tr>
    <tr>
        <td style="width: 100px">
            Profile Image
        </td>
        <td>
            <asp:Image ID="imgOtherProfile" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Name
        </td>
        <td>
            <asp:Label ID="lblOtherName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Twitter Id
        </td>
        <td>
            <asp:Label ID="lblOtherTwitterId" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Screen Name
        </td>
        <td>
            <asp:Label ID="lblOtherScreenName" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Description
        </td>
        <td>
            <asp:Label ID="lblOtherDescription" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Tweets
        </td>
        <td>
            <asp:Label ID="lblOtherTweets" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Followers
        </td>
        <td>
            <asp:Label ID="lblOtherFollowers" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Friends
        </td>
        <td>
            <asp:Label ID="lblOtherFriends" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Favorites
        </td>
        <td>
            <asp:Label ID="lblOtherFavorites" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            Location
        </td>
        <td>
            <asp:Label ID="lblOtherLocation" runat="server" />
        </td>
    </tr>
</table>
 
 
Namespaces
You will need to import the following Namespaces.
C#
using System.Data;
using ASPSnippets.TwitterAPI;
 
VB.Net
Imports System.Data
Imports ASPSnippets.TwitterAPI
 
 
Login with Twitter Account and fetch the Twitter Account and Profile Details
Firstly we will need to authorize the user using Twitter API and get the Access Token so that we can tweet the status message on Twitter. You will notice that I have specified placeholders for Twitter API Key and Secret, you need to replace it with that of the Twitter API Key and Secret of your Twitter App.
In the below code we are authorizing the user using Twitter and also asking him to provide permission to the application to fetch the user Twitter Account and Profile Details.
Once the user authorizes the application and grants access to his profile and account details, a call is made to the FetchProfile method of the ASPSnippets Twitter API.
This function works in two ways, when the Twitter Name or Handle is not passed it returns profile of the Logged in user, and when the Twitter Name or Handle is passed then it returns the profile of the corresponding user.
The user profile and account details will be returned as DataTable which can be easily displayed as shown below.
C#
protected void Page_Load(object sender, EventArgs e)
{
    TwitterConnect.API_Key = "<Twitter API Key>";
    TwitterConnect.API_Secret = "<Twitter API Secret>";
    if (!IsPostBack)
    {
        if (TwitterConnect.IsAuthorized)
        {
            TwitterConnect twitter = new TwitterConnect();
           
            //LoggedIn User Twitter Profile Details
            DataTable dt = twitter.FetchProfile();
            imgProfile.ImageUrl = dt.Rows[0]["profile_image_url"].ToString();
            lblName.Text = dt.Rows[0]["name"].ToString();
            lblTwitterId.Text = dt.Rows[0]["Id"].ToString();
            lblScreenName.Text = dt.Rows[0]["screen_name"].ToString();
            lblDescription.Text = dt.Rows[0]["description"].ToString();
            lblTweets.Text = dt.Rows[0]["statuses_count"].ToString();
            lblFollowers.Text = dt.Rows[0]["followers_count"].ToString();
            lblFriends.Text = dt.Rows[0]["friends_count"].ToString();
            lblFavorites.Text = dt.Rows[0]["favourites_count"].ToString();
            lblLocation.Text = dt.Rows[0]["location"].ToString();
            tblTwitter.Visible = true;
 
            //Any other User Twitter Profile Details. Here jQueryFAQs
            dt = twitter.FetchProfile("jQueryFAQs");
            imgOtherProfile.ImageUrl = dt.Rows[0]["profile_image_url"].ToString();
            lblOtherName.Text = dt.Rows[0]["name"].ToString();
            lblOtherTwitterId.Text = dt.Rows[0]["Id"].ToString();
            lblOtherScreenName.Text = dt.Rows[0]["screen_name"].ToString();
            lblOtherDescription.Text = dt.Rows[0]["description"].ToString();
            lblOtherTweets.Text = dt.Rows[0]["statuses_count"].ToString();
            lblOtherFollowers.Text = dt.Rows[0]["followers_count"].ToString();
            lblOtherFriends.Text = dt.Rows[0]["friends_count"].ToString();
            lblOtherFavorites.Text = dt.Rows[0]["favourites_count"].ToString();
            lblOtherLocation.Text = dt.Rows[0]["location"].ToString();
            tblOtherTwitter.Visible = true;
 
            btnLogin.Enabled = false;
        }
        if (TwitterConnect.IsDenied)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('User has denied access.')", true);
        }
    }
}
 
protected void btnLogin_Click(object sender, EventArgs e)
{
    if (!TwitterConnect.IsAuthorized)
    {
        TwitterConnect twitter = new TwitterConnect();
        twitter.Authorize(Request.Url.AbsoluteUri.Split('?')[0]);
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    TwitterConnect.API_Key = "<Twitter API Key>"
    TwitterConnect.API_Secret = "<Twitter API Secret>"
    If Not IsPostBack Then
            If TwitterConnect.IsAuthorized Then
                Dim twitter As New TwitterConnect()
 
                'LoggedIn User Twitter Profile Details
                Dim dt As DataTable = twitter.FetchProfile()
                imgProfile.ImageUrl = dt.Rows(0)("profile_image_url").ToString()
                lblName.Text = dt.Rows(0)("name").ToString()
                lblTwitterId.Text = dt.Rows(0)("Id").ToString()
                lblScreenName.Text = dt.Rows(0)("screen_name").ToString()
                lblDescription.Text = dt.Rows(0)("description").ToString()
                lblTweets.Text = dt.Rows(0)("statuses_count").ToString()
                lblFollowers.Text = dt.Rows(0)("followers_count").ToString()
                lblFriends.Text = dt.Rows(0)("friends_count").ToString()
                lblFavorites.Text = dt.Rows(0)("favourites_count").ToString()
                lblLocation.Text = dt.Rows(0)("location").ToString()
                tblTwitter.Visible = True
 
                'Any other User Twitter Profile Details. Here jQueryFAQs
                dt = twitter.FetchProfile("jQueryFAQs")
                imgOtherProfile.ImageUrl = dt.Rows(0)("profile_image_url").ToString()
                lblOtherName.Text = dt.Rows(0)("name").ToString()
                lblOtherTwitterId.Text = dt.Rows(0)("Id").ToString()
                lblOtherScreenName.Text = dt.Rows(0)("screen_name").ToString()
                lblOtherDescription.Text = dt.Rows(0)("description").ToString()
                lblOtherTweets.Text = dt.Rows(0)("statuses_count").ToString()
                lblOtherFollowers.Text = dt.Rows(0)("followers_count").ToString()
                lblOtherFriends.Text = dt.Rows(0)("friends_count").ToString()
                lblOtherFavorites.Text = dt.Rows(0)("favourites_count").ToString()
                lblOtherLocation.Text = dt.Rows(0)("location").ToString()
                tblOtherTwitter.Visible = True
 
                btnLogin.Enabled = False
            End If
            If TwitterConnect.IsDenied Then
                ClientScript.RegisterStartupScript(Me.[GetType](), "key", "alert('User has denied access.')", True)
            End If
        End If
    End If
End Sub
 
Protected Sub btnLogin_Click(sender As Object, e As EventArgs)
    If Not TwitterConnect.IsAuthorized Then
        Dim twitter As New TwitterConnect()
        twitter.Authorize(Request.Url.AbsoluteUri.Split("?"c)(0))
    End If
End Sub
 
 
Screenshots
Twitter Integration in ASP.Net website using C# and VB.Net
 
Twitter Integration in ASP.Net website using C# and VB.Net
 
Twitter Integration in ASP.Net website using C# and VB.Net
 
 
Demo
 
 
Downloads