Twitter from ASP.NET – Simple GUI

trav-037 Yesterday I posted a short description of how to get Twitterizer installed in a web app.  We left off making sure that the DLL was installed correctly but that’s about as far as we got.

Today I want to extend the application to allow you to login using the Login control and post a message.

So the first thing we want to do today is to add a login page.  We will add the login control to this page so that we can login to Twitter from our page.  I’m assuming you know how to do this much on your own.

In order to make all of the pages require a login except for the login.aspx page, replace the following in your web.config file

    <authentication mode="Windows"/>

with

    <authorization>
      <deny users="?"/>
    </authorization>
    <authentication mode="Forms"/>

On the codebehind of the Login page add the following using statement

using Twitterizer.Framework;

And create an event handler for the Login control’s Authenticate event

protected void Login1_Authenticate
    (object sender, AuthenticateEventArgs e)
{
    e.Authenticated = Twitter.VerifyCredentials
        (Login1.UserName, Login1.Password);
    if (e.Authenticated)
        Session["password"] = Login1.Password;
}

You’ll notice that since all I really want to know is that I can login, I’m using the static method VerifyCredentials() to attempt to login.  I’m not creating the object like we did yesterday.

You’ll also notice that I’m storing the password in a session variable.  You might want to store it encrypted in a cookie instead.  But since this is just a demo, this should work fine as long as you are using some sort of session server.

On the default page we are going to add a text field and a submit button.  The click event for our Submit button will grab the contents of the text field and send it all up to Twitter as a status message.

Add the Twitterizer using statement to the top of the Default codebehind page like we did for the login page.

Then put the following code in the click event handler

protected void Button1_Click
    (object sender, EventArgs e)
{
    String password =
        Session["Password"].ToString();
    String name = Page.User.Identity.Name;
    (new Twitter(name,password))
        .Status.Update(TextBox1.Text);
    // Clear out the text box.
    TextBox1.Text = string.Empty;
}

Notice that we need the username and password again because the server knows nothing of the previous round trip.  We retrieve the username from the User.Identity object that is hanging off of the Page.  The password we retrieve from our session variable that we created when we logged in.

Don’t forget to remove the hard-coded message stuff we put in yesterday or you’ll end up with two messages in a row–one from yesterday’s code and the new message that you will place in the text box.

There is obviously a bit of validation that you might want to add to this to make it production code, but we are focusing on accessing Twitter here, not how to write production code.

If it all works, you should now be able to login and post a message.

Related Post

5 Responses to “Twitter from ASP.NET – Simple GUI”

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear