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.


Other Related Items:

Don't Mess With Me, I am a Programmer - T-Shirt (40 colors)Don't Mess With Me, I am a Programmer - T-Shirt (40 colors)Cotton T-Shirt 5.6 oz., heavyweight 100% preshrunk cotton T-shirt, Quarter-turned. Seamless collar, Taped shoulder-to-shoulder, Double-needle stitched neck, sleeves and hemmed bottom. Direct printed on Front of T-Shirt, this is not a transfer. Won't peel or crack in washer/dryer.
Apache 2 Pocket Reference: For Apache Programmers & Administrators (Pocket Reference (O'Reilly))Apache 2 Pocket Reference: For Apache Programmers & Administrators (Pocket Reference (O'Reilly))

Even if you know the Apache web server inside and out, you still need an occasional on-the-job reminder -- especially if you're moving to the newer... Read More >

Knee Length Tiered Sleek Stretch Skirt ( Choose Charcoal or Brown )Knee Length Tiered Sleek Stretch Skirt ( Choose Charcoal or Brown )This versatile skirt can be dressed up or down. Pair with a sexy blouse and heels for a night on the town or pair with a cardigan and flats for work. ... Read More >

Related Post

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

  • Nice and neat tutorial. Thanks Dave!

  • [...] from ASP.NET and Twitter from ASP.NET – Simple GUI (Dave M. [...]

  • very nice tutorial, it help me a lot,
    Thanks Dave…

  • Your blog is informative ? keep up the good work!!!!

  • Mike:

    HI Well I am very very new to asp an well i am not sure where to every start is ther any way that you can set this all up so that i can look at it an see what your doing to call what an all that from what I Read you need one file called web.config then on a loging part of the page here ever that go’s well then I just got lost lol is there a way you can show me the code what i mean is a demo of all it in a file an info on what gos where an how have little info on what you did an what the code does I know that I am asking you to go back to the very basics but I would like to learn asp it looks real neat I do php an vb but never played with this yet thanks Mike

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor