User Authentication
12 Jun 2009 @ 03:34PM

Updated: 26 Jan 2010 @ 08:44AM
Now that we have basic functionality out of the way, we need to allow someone to actually write, edit and delete blogs. In order to do that, we need to implement user authentication. So let's begin by making the login form. We'll start with making some changes to the layout() method.
private string layout()
{
     string content = @" <center><img src='header.png'></center>
          <div class='rightBar'>
               <div class='title'>C# Blog Menu</div>
               <ul>
                    <li><a href='default.aspx?logon=true'>Log In </a></li>
                    <li><a href=''>Create Account</a></li>
                    <li><a href=''>Write Blog Entry</a></li>
                    <li><a href=''>See New Entries</a></li>
               </ul>
          </div>"
;
     return content;
}

All I've done is add a link back to the same page with a GET variable of logon=true. This will do for now, though eventually we'll change it.
Comments (0)
With that done, let's make a change to the Page_Load() method.
protected void Page_Load(object sender, EventArgs e)
{
     string content = null;
     content += header();
     content += layout();

     if (getVariable("logon", var.GET) == "true")
     {
          content += showLogon();
     }
     else
     {
          content += showBlogs();
     }
     content += footer();
     Response.Write(content);
}

Ok, so we've created an if/else decision block that looks at the results of getVariable(). If a GET variable called logon equals true, then we append the results of the showLogon() method to our content. If there is no GET variable called logon, or it's any value other than true, we do the showBlogs() method. This lets us show a logon dialog instead of the blog list. There are a few new things here.
Comments (0)
First, let's look at the getVariable() method. This is very similar to the one we covered a few tutorials ago.
private string getVariable(string input, var varType)
{
     string variable = null;
     switch (varType)
     {
          case var.GET:
               try
               {
                    int doesVarExist = Request.QueryString[input].Length;
                    variable = Request.QueryString[input];
               }
               catch { }
               break;
     }

     return variable;
}

So here we've set up a case. Both in this method and where we called it, you may notice a second variable of type var. This is a custom variable type... we'll get to that in a moment. Within the getVariable() method I then set up a case statement to test if this variable is of type var.GET. If it is, I do the standard code to retrieve a get variable and then return it. Later on this method will grow to include other cases. So what's with the var variable type?
public enum [CYAN]var[/CYAN]
{
     GET, POST, SESSION, COOKIE
}

I defined the above in my code, right above Page_Load() and beneath the public partial class definition. I'll make the code available after we get done with user authentication so you can see the whole thing. Regardless, what I'm doing here is defining a new public variable of type enum and calling it var. Enum is short for enumeration and basically it defines a series of different possibilities. In this case, we have var.GET, var.POST, var.SESSION and var.COOKIE. Eventually these should all be defined in that getVariable() method. Why? They are all ways in which data can be passed to us across web pages. You'll see as we continue working on the user authentication.
Comments (0)
Next we have the actual showLogon() method.
private string showLogon()
{
     string content = @"<center><div class='login'>
          <form method='POST' action='default.aspx'>
               Username: <input name='username'><br>
               Password: <input name='password' type='password'><br>
               <div class='button'><input type='submit' value='Log In'></div>
          </form>
     </div></center>"
;

     return content;
}

As you can see, there's not much to this method. We're just putting out a small form asking for a username and password. This is then sent back to default.aspx (our current page) with a method of POST.
Comments (0)
So, let's go back to our getVariable() method and add POST in.
private string getVariable(string input, var varType)
{
     string variable = null;
     switch (varType)
     {
          case var.GET:
               try
               {
                    int doesVarExist = Request.QueryString[input].Length;
                    variable = Request.QueryString[input];
               }
               catch { }
               break;
          case var.POST:
               try
               {
                    if (Request.Form.Get(input).Length > 0)
                    {
                         variable = Request.Form.Get(input);
                    }
               }
               catch { }
               break;
     }
     
     return variable;
}

Nothing especially new here. Now that we can get the POST vars we POSTed, on to the fun stuff. We need to check for these POST vars and then process a login. Continue to the next page for more breath-taking programming.
Comments (0)